均为 Simple 难度的水题。

二叉树的中序遍历

题目[94]:给定一个二叉树,返回它的中序 遍历。

解题思路:Too simple.

  1. class Solution
  2. {
  3. public:
  4. vector<int> inorderTraversal(TreeNode *root)
  5. {
  6. return inorderNonRec(root);
  7. vector<int> v;
  8. innerTraversal(root, v);
  9. return v;
  10. }
  11. void innerTraversal(TreeNode *p, vector<int> &v)
  12. {
  13. if (p == nullptr)
  14. return;
  15. innerTraversal(p->left, v);
  16. v.push_back(p->val);
  17. innerTraversal(p->right, v);
  18. }
  19. vector<int> inorderNonRec(TreeNode *root)
  20. {
  21. vector<int> v;
  22. if (root != nullptr)
  23. {
  24. stack<TreeNode *> s;
  25. auto p = root;
  26. while (!s.empty() || p != nullptr)
  27. {
  28. if (p != nullptr)
  29. {
  30. s.push(p);
  31. p = p->left;
  32. }
  33. else
  34. {
  35. p = s.top(), s.pop();
  36. v.push_back(p->val);
  37. p = p->right;
  38. }
  39. }
  40. }
  41. return v;
  42. }
  43. };

相同的树

题目[100]:给定两个二叉树,编写一个函数来检验它们是否相同。如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

示例

  1. 输入: 1 1
  2. / \ / \
  3. 2 3 2 3
  4. [1,2,3], [1,2,3]
  5. 输出: true

解题思路:递归。

  1. #include "leetcode.h"
  2. class Solution
  3. {
  4. public:
  5. bool isSameTree(TreeNode *p, TreeNode *q)
  6. {
  7. return innerCheck(p, q);
  8. }
  9. bool innerCheck(TreeNode *p, TreeNode *q)
  10. {
  11. if ((p == nullptr) ^ (q == nullptr))
  12. return false;
  13. if (p == nullptr && q == nullptr)
  14. return true;
  15. if (p->val != q->val)
  16. return false;
  17. return innerCheck(p->left, q->left) && innerCheck(p->right, q->right);
  18. }
  19. };

对称二叉树

题目[101]:给定一个二叉树,检查它是否是镜像对称的。

示例

  1. input:
  2. 1
  3. / \
  4. 2 2
  5. / \ / \
  6. 3 4 4 3
  7. output: true

解题思路:递归。

  1. class Solution
  2. {
  3. public:
  4. bool isSymmetric(TreeNode *root)
  5. {
  6. if (root == nullptr)
  7. return true;
  8. return innerCheck(root->left, root->right);
  9. }
  10. bool innerCheck(TreeNode *p, TreeNode *q)
  11. {
  12. if ((p == nullptr) ^ (q == nullptr))
  13. return false;
  14. if (p == nullptr)
  15. return true;
  16. if (p->val != q->val)
  17. return false;
  18. return innerCheck(p->left, q->right) && innerCheck(p->right, q->left);
  19. }
  20. };

二叉树的最大深度

题目[104]:给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

示例

  1. input:
  2. 3
  3. / \
  4. 9 20
  5. / \
  6. 15 7
  7. output: 3

解题思路:DFS 。

  1. #define max(a, b) ((a) > (b) ? (a) : (b))
  2. class Solution
  3. {
  4. public:
  5. int maxDepth(TreeNode *root)
  6. {
  7. return dfs(root);
  8. }
  9. int dfs(TreeNode *p)
  10. {
  11. if (p == nullptr)
  12. return 0;
  13. int a = dfs(p->left), b = dfs(p->right);
  14. return max(a, b) + 1;
  15. }
  16. };

值得注意的是,不能 return max(dfs(p->lect), dfs(p->right)) + 1,因为宏展开后就会执行 4 次 DFS 。

二叉树的层次遍历 II

题目[107]:给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)。

示例

  1. Input:
  2. 3
  3. / \
  4. 9 20
  5. / \
  6. 15 7
  7. Output:
  8. [[15,7], [9,20], [3]]

解题思路:使用队列进行层次遍历,同时记下层数,使用 map<int,vector> 记录各个层次的节点。

  1. struct Tuple
  2. {
  3. TreeNode *ptr;
  4. int level;
  5. Tuple(TreeNode *q = nullptr, int l = -1) : ptr(q), level(l) {}
  6. };
  7. class Solution
  8. {
  9. public:
  10. vector<vector<int>> levelOrderBottom(TreeNode *root)
  11. {
  12. if (root == nullptr)
  13. return vector<vector<int>>();
  14. map<int, vector<int>> m;
  15. queue<Tuple> q;
  16. q.push(Tuple(root, 0));
  17. while (!q.empty())
  18. {
  19. Tuple p = q.front();
  20. q.pop();
  21. m[p.level].push_back(p.ptr->val);
  22. if (p.ptr->left)
  23. q.push(Tuple(p.ptr->left, p.level + 1));
  24. if (p.ptr->right)
  25. q.push(Tuple(p.ptr->right, p.level + 1));
  26. }
  27. vector<vector<int>> v;
  28. for (auto x : m)
  29. v.push_back(x.second);
  30. return vector<vector<int>>(v.rbegin(), v.rend());
  31. }
  32. };

将有序数组转换为二叉搜索树

题目[108]:将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。

示例

  1. 给定有序数组: [-10,-3,0,5,9],
  2. 一个可能的答案是:[0,-3,9,-10,null,5],它可以表示下面这个高度平衡二叉搜索树:
  3. 0
  4. / \
  5. -3 9
  6. / /
  7. -10 5

解题思路:二叉搜索树的性质是中序遍历呈升序,所以数组的中间元素 nums[mid] 必然是二叉树的根节点。所以 [start, mid - 1] 是左子树,[mid + 1, end] 是右子树,递归处理。如果数组长度为偶数,中间元素有 2 个,可任意取一个为根节点。


  1. class Solution
  2. {
  3. public:
  4. TreeNode *sortedArrayToBST(vector<int> &nums)
  5. {
  6. TreeNode *root = nullptr;
  7. innerCreate(nums, 0, nums.size() - 1, root);
  8. return root;
  9. }
  10. void innerCreate(vector<int> &v, int start, int end, TreeNode *&p)
  11. {
  12. if (start > end)
  13. return;
  14. int mid = start + (end - start) / 2;
  15. p = new TreeNode(v[mid]);
  16. innerCreate(v, start, mid - 1, p->left);
  17. innerCreate(v, mid + 1, end, p->right);
  18. }
  19. };

平衡二叉树

题目[110]:给定一个二叉树,判断它是否是高度平衡的二叉树。

示例

  1. Input: [3,9,20,null,null,15,7]
  2. 3
  3. / \
  4. 9 20
  5. / \
  6. 15 7
  7. Output: true

解题思路:

  • 暴力解法

    1. #include <cmath>
    2. class Solution
    3. {
    4. public:
    5. bool isBalanced(TreeNode *root)
    6. {
    7. return forceSolution(root);
    8. }
    9. // brute force solution
    10. bool forceSolution(TreeNode *p)
    11. {
    12. if (p == nullptr)
    13. return true;
    14. bool flag = abs(height(p->left) - height(p->right)) <= 1;
    15. return flag && forceSolution(p->left) && forceSolution(p->right);
    16. }
    17. int height(TreeNode *p)
    18. {
    19. if (p == nullptr)
    20. return 0;
    21. return max(height(p->left), height(p->right)) + 1;
    22. }
    23. };

    请注意一点细节,flag && forceSolution(p->left) && forceSolution(p->right) 效率要比 forceSolution(p->left) && forceSolution(p->right) && flag 高。

    显然,暴力解法对求高度存在需要「冗余」的情况,比如,我们知道 h(left) = height(p->left),那么 h(p) = h(left) + 1,但是暴力解法仍然用 h(p) = height(p)

  • 自底向上的递归

    返回值表示以 p 为根的子树是否平衡,height 记录以 p 为根的子树的高度。

    1. bool isBalanced(TreeNode *root)
    2. {
    3. int height = 0;
    4. return innerIsBalanced(root, height);
    5. }
    6. bool innerIsBalanced(TreeNode *p, int &height)
    7. {
    8. if (p == nullptr)
    9. {
    10. height = 0;
    11. return true;
    12. }
    13. int lh = 0, rh = 0;
    14. if (innerIsBalanced(p->left, lh) && innerIsBalanced(p->right, rh) && abs(lh - rh) <= 1)
    15. {
    16. height = max(lh, rh) + 1;
    17. return true;
    18. }
    19. return false;
    20. }

二叉树的最小深度

题目[111]:给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

示例

  1. Input:
  2. 3
  3. / \
  4. 9 20
  5. / \
  6. 15 7
  7. Output: 2

解题思路:记录每个节点层数的层次遍历(实质上是 BFS)。第一个叶子节点的层数就是答案。

  1. class Solution
  2. {
  3. public:
  4. int minDepth(TreeNode *root)
  5. {
  6. return (root == nullptr) ? 0 : bfs(root);
  7. }
  8. int bfs(TreeNode *root)
  9. {
  10. typedef pair<TreeNode *, int> Node;
  11. queue<Node> q;
  12. q.push(Node(root, 1));
  13. while (!q.empty())
  14. {
  15. auto &node = q.front();
  16. q.pop();
  17. if (node.first->left == nullptr && node.first->right == nullptr)
  18. return node.second;
  19. if (node.first->left != nullptr)
  20. q.push(Node(node.first->left, node.second + 1));
  21. if (node.first->right != nullptr)
  22. q.push(Node(node.first->right, node.second + 1));
  23. }
  24. return -1;
  25. }
  26. };

路径总和

题目[112]:给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。

示例

  1. Input: sum = 22
  2. 5
  3. / \
  4. 4 8
  5. / / \
  6. 11 13 4
  7. / \ \
  8. 7 2 1
  9. Output: true, because sum(5->4->11->2) = 22

解题思路:回溯法。current 记录当前的遍历路径的和。

  1. class Solution
  2. {
  3. public:
  4. bool hasPathSum(TreeNode *root, int sum)
  5. {
  6. bool result = false;
  7. innerSum(root, sum, 0, result);
  8. return result;
  9. }
  10. void innerSum(TreeNode *p, int target, int current, bool &result)
  11. {
  12. if (p == nullptr)
  13. return;
  14. current += p->val;
  15. if (current == target && p->left == nullptr && p->right == nullptr)
  16. {
  17. result = true;
  18. return;
  19. }
  20. innerSum(p->left, target, current, result);
  21. if (!result)
  22. innerSum(p->right, target, current, result);
  23. }
  24. };

翻转二叉树

题目[226]:翻转一棵二叉树。

示例

  1. Input:
  2. 4
  3. / \
  4. 2 7
  5. / \ / \
  6. 1 3 6 9
  7. Output:
  8. 4
  9. / \
  10. 7 2
  11. / \ / \
  12. 9 6 3 1

解题思路:对每个节点执行 swap(p->left, p->right)TreeNode* &p 表示的是指针的引用。

  1. class Solution
  2. {
  3. public:
  4. TreeNode *invertTree(TreeNode *root)
  5. {
  6. if (root != nullptr)
  7. innerInvert(root->left, root->right);
  8. return root;
  9. }
  10. void innerInvert(TreeNode *&l, TreeNode *&r)
  11. {
  12. auto p = l;
  13. l = r;
  14. r = p;
  15. if (l != nullptr)
  16. innerInvert(l->left, l->right);
  17. if (r != nullptr)
  18. innerInvert(r->left, r->right);
  19. }
  20. };

二叉搜索树的最近公共祖先

题目[235]:给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。

示例

  1. 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
  2. 输出: 6
  3. 解释: 节点 2 和节点 8 的最近公共祖先是 6

解题思路:利用二叉搜索树的性质,左子树 < 根 < 右子树。那么:

  • p.val < root.val && q.val < root.val:在左子树搜索。
  • p.val > root.val && q.val < root.val:在右子树搜索。
  • 其他情况:root 就是公共祖先。

递归解法

  1. TreeNode *lca(TreeNode *root, TreeNode *p, TreeNode *q)
  2. {
  3. if (p->val < root->val && q->val < root->val)
  4. return lca(root->left, p, q);
  5. else if (p->val > root->val && q->val > root->val)
  6. return lca(root->right, p, q);
  7. else
  8. return root;
  9. }

非递归解法

  1. TreeNode *lca2(TreeNode *root, TreeNode *p, TreeNode *q)
  2. {
  3. auto node = root;
  4. while (node != nullptr)
  5. {
  6. if (p->val < node->val && q->val < node->val)
  7. node = node->left;
  8. else if (p->val > node->val && q->val > node->val)
  9. node = node->right;
  10. else
  11. break;
  12. }
  13. return node;
  14. }

二叉树的所有路径

题目[257]:给定一个二叉树,返回所有从根节点到叶子节点的路径。

示例

  1. 输入:
  2. 1
  3. / \
  4. 2 3
  5. \
  6. 5
  7. 输出: ["1->2->5", "1->3"]
  8. 解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3

解题思路:显而易见的回溯法(实际上也是二叉树的遍历),如果找到叶子节点,说明是一个完整的路径。

  1. #include "leetcode.h"
  2. class Solution
  3. {
  4. public:
  5. vector<string> result;
  6. vector<string> binaryTreePaths(TreeNode *root)
  7. {
  8. if (root != nullptr)
  9. preorder(root, "");
  10. return result;
  11. }
  12. void preorder(TreeNode *p, string s)
  13. {
  14. bool l = (p->left != nullptr);
  15. bool r = (p->right != nullptr);
  16. if (l || r)
  17. s += to_string(p->val) + "->";
  18. else if (!l && !r)
  19. {
  20. s += to_string(p->val);
  21. result.push_back(s);
  22. return;
  23. }
  24. if (l)
  25. preorder(p->left, s);
  26. if (r)
  27. preorder(p->right, s);
  28. }
  29. };

左叶子之和

题目[404]:计算给定二叉树的所有左叶子之和。

示例

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7
  6. 在这个二叉树中,有两个左叶子,分别是 9 15,所以返回 24

解题思路:遍历过程使用一个 flag 来表示本次节点是否为左子树。如果既是左子树,又是叶子节点,就是要累加的节点。

  1. #include "leetcode.h"
  2. class Solution
  3. {
  4. public:
  5. int sum = 0;
  6. int sumOfLeftLeaves(TreeNode *root)
  7. {
  8. if (root != nullptr)
  9. preorder(root, false);
  10. return sum;
  11. }
  12. void preorder(TreeNode *root, bool isLeft)
  13. {
  14. bool l = (root->left != nullptr);
  15. bool r = (root->right != nullptr);
  16. if (isLeft && !l && !r)
  17. sum += root->val;
  18. if (l)
  19. preorder(root->left, true);
  20. if (r)
  21. preorder(root->right, false);
  22. }
  23. };

路径总和 III

题目[437]:给定一个二叉树,它的每个结点都存放着一个整数值。找出路径和等于给定数值的路径总数。路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数。

示例

  1. root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
  2. 10
  3. / \
  4. 5 -3
  5. / \ \
  6. 3 2 11
  7. / \ \
  8. 3 -2 1
  9. 返回 3。和等于 8 的路径有:
  10. 1. 5 -> 3
  11. 2. 5 -> 2 -> 1
  12. 3. -3 -> 11

解题思路:将二叉树的每一个完整路径看作是一个数组 nums(假设第一个元素是 nums[1]),那么本题就是要找到 sum(i, j) = sum 的下标 i 和 j 。

为此,使用一个数组 vv[0] = 0v[i] 表示 sum(nums[1] ... nums[i]) ,即 nums 前 i 个元素的和。那么 sum(nums[i] ... nums[j]) = v[j] - v[i - 1]

使用先序遍历每一个从根到叶子的路径。

  1. class Solution
  2. {
  3. public:
  4. int result = 0;
  5. int pathSum(TreeNode *root, int sum)
  6. {
  7. if (root == nullptr)
  8. return 0;
  9. int d = depth(root);
  10. vector<int> v(d + 1);
  11. preorder(1, v, root, sum);
  12. return result;
  13. }
  14. int depth(TreeNode *p)
  15. {
  16. if (p == nullptr)
  17. return 0;
  18. return max(depth(p->left), depth(p->right)) + 1;
  19. }
  20. void preorder(int idx, vector<int> &v, TreeNode *p, const int sum)
  21. {
  22. if (p == nullptr)
  23. return;
  24. v[idx] = v[idx - 1] + p->val;
  25. for (int i = 0; i < idx; i++)
  26. {
  27. if (v[idx] - v[i] == sum)
  28. result++;
  29. }
  30. preorder(idx + 1, v, p->left, sum);
  31. preorder(idx + 1, v, p->right, sum);
  32. }
  33. };

二叉搜索树中的众数

题目[501]:给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。

示例

  1. Input:
  2. 1
  3. \
  4. 2
  5. /
  6. 2
  7. Output: [2]

解题思路:利用BST的性质,中序遍历为升序序列。current 记录当前数字 number 出现的次数,last 记录上一次找到的「候选众数」出现的次数。

  1. class Solution
  2. {
  3. public:
  4. vector<int> v;
  5. int current = 0;
  6. int last = 0;
  7. int number = 0x80000000;
  8. vector<int> findMode(TreeNode *root)
  9. {
  10. if (root != nullptr)
  11. inorder(root);
  12. return v;
  13. }
  14. void inorder(TreeNode *p)
  15. {
  16. if (p == nullptr)
  17. return;
  18. inorder(p->left);
  19. if (last == 0)
  20. last = 1;
  21. if (p->val != number)
  22. current = 0;
  23. number = p->val;
  24. current++;
  25. if (current == last)
  26. v.push_back(number);
  27. if (current > last)
  28. {
  29. last = current;
  30. v.clear(), v.push_back(number);
  31. }
  32. inorder(p->right);
  33. }
  34. };

二叉搜索树的最小绝对差

题目[530]:给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值。

示例

  1. 输入:
  2. 1
  3. \
  4. 3
  5. /
  6. 2
  7. 输出:1
  8. 解释:最小绝对差为 1,其中 2 1 的差的绝对值为 1(或者 2 3)。

解题思路:BST中序遍历呈升序。

  1. #include "leetcode.h"
  2. class Solution
  3. {
  4. public:
  5. int result = 0x7ffffff;
  6. int pre = 0x7fffffff;
  7. int getMinimumDifference(TreeNode *root)
  8. {
  9. inorder(root);
  10. return result;
  11. }
  12. void inorder(TreeNode *p)
  13. {
  14. if (p == nullptr)
  15. return;
  16. inorder(p->left);
  17. result = min(result, abs(p->val - pre));
  18. pre = p->val;
  19. inorder(p->right);
  20. }
  21. };

把二叉搜索树转换为累加树

题目[538]:给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和。

示例

  1. 输入: 原始二叉搜索树:
  2. 5
  3. / \
  4. 2 13
  5. 输出: 转换为累加树:
  6. 18
  7. / \
  8. 20 13

解题思路:逆中序遍历 BST 。

  1. class Solution
  2. {
  3. public:
  4. int sum = 0;
  5. TreeNode *convertBST(TreeNode *root)
  6. {
  7. postorder(root);
  8. return root;
  9. }
  10. void postorder(TreeNode *p)
  11. {
  12. if (p == nullptr)
  13. return;
  14. postorder(p->right);
  15. sum += p->val;
  16. p->val = sum;
  17. postorder(p->left);
  18. }
  19. };

二叉树的直径

题目[534]:给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。

示例

  1. Input:
  2. 1
  3. / \
  4. 2 3
  5. / \
  6. 4 5
  7. Output: 3

解题思路:所谓直径,就是二叉树中任意路径上的节点数减一。

对于二叉树中的每个节点 node,以 node 为根的子树,其直径为 depth(node.left) + depth(node.right)

  • 自顶向下的递归

    1. int result = 0;
    2. int diameterOfBinaryTree(TreeNode *root)
    3. {
    4. preorder(root);
    5. return result;
    6. }
    7. int depth(TreeNode *p)
    8. {
    9. return p == nullptr ? 0 : max(depth(p->left), depth(p->right)) + 1;
    10. }
    11. void preorder(TreeNode *p)
    12. {
    13. if (p == nullptr)
    14. return;
    15. result = max(result, depth(p->left) + depth(p->right));
    16. preorder(p->left);
    17. preorder(p->right);
    18. }
  • 自底向上的递归

    显然,对每个节点都调用一次 depth 函数,有很多冗余的遍历。求出每个节点的高度,实际上只需要一次自底向上的遍历。因为 depth(p) = max(depth(p.left), depth(p.right)) + 1 。因此可使用后序遍历。

    1. int result = 0;
    2. int diameterOfBinaryTree(TreeNode *root)
    3. {
    4. int height = 0;
    5. bottom2top(root, height);
    6. return result;
    7. }
    8. void bottom2top(TreeNode *p, int &height)
    9. {
    10. if (p == nullptr)
    11. {
    12. height = 0;
    13. return;
    14. }
    15. int l = height, r = height;
    16. bottom2top(p->left, l);
    17. bottom2top(p->right, r);
    18. height = max(l, r) + 1;
    19. result = max(result, l + r);
    20. }

二叉树的坡度

题目[563]:给定一个二叉树,计算整个树的坡度。一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值。空结点的的坡度是0。整个树的坡度就是其所有节点的坡度之和。

示例

  1. 输入:
  2. 1
  3. / \
  4. 2 3
  5. 输出: 1
  6. 解释:
  7. 结点的坡度 2 : 0
  8. 结点的坡度 3 : 0
  9. 结点的坡度 1 : |2-3| = 1
  10. 树的坡度 : 0 + 0 + 1 = 1

解题思路:实际上要解决的问题是怎么求出每个子树的和。显然还是采取自底向上的后序遍历。

  1. class Solution
  2. {
  3. public:
  4. int tilt = 0;
  5. int findTilt(TreeNode *root)
  6. {
  7. int sum = 0;
  8. postorder(root, sum);
  9. return tilt;
  10. }
  11. void postorder(TreeNode *p, int &sum)
  12. {
  13. if (p == nullptr)
  14. {
  15. return;
  16. }
  17. int l = sum, r = sum;
  18. postorder(p->left, l);
  19. postorder(p->right, r);
  20. sum += p->val + l + r;
  21. tilt += abs(l - r);
  22. }
  23. };

另一个树的子树

题目[572]:给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。

解题思路:暴力解法。先实现 isSame(s, t) 判断 st 是否完全相等,再遍历 s 的每一个节点 p ,判断 isSame(p, t)

  1. class Solution
  2. {
  3. public:
  4. bool isSubtree(TreeNode *s, TreeNode *t)
  5. {
  6. if (s == nullptr)
  7. return t == nullptr;
  8. queue<TreeNode *> q;
  9. q.push(s);
  10. while (!q.empty())
  11. {
  12. auto p = q.front();
  13. q.pop();
  14. if (isSame(p, t))
  15. return true;
  16. if (p->left != nullptr)
  17. q.push(p->left);
  18. if (p->right != nullptr)
  19. q.push(p->right);
  20. }
  21. return false;
  22. }
  23. bool isSame(TreeNode *s, TreeNode *t)
  24. {
  25. if ((s == nullptr) ^ (t == nullptr))
  26. return false;
  27. if (s == nullptr && t == nullptr)
  28. return true;
  29. return (s->val == t->val) && isSame(s->left, t->left) && isSame(s->right, t->right);
  30. }
  31. };

[leetcode] 树 -Ⅰ的更多相关文章

  1. LeetCode树专题

    LeetCode树专题 98. 验证二叉搜索树 二叉搜索树,每个结点的值都有一个范围 /** * Definition for a binary tree node. * struct TreeNod ...

  2. leetcode 树类型题

    树的测试框架: // leetcodeTree.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...

  3. leetcode: 树

    1. sum-root-to-leaf-numbers Given a binary tree containing digits from0-9only, each root-to-leaf pat ...

  4. Leetcode 树(102, 637)

    637: 二叉树的层平均值 给定一个非空二叉树,返回一个由每层节点平均值组成的数组: https://leetcode-cn.com/problems/average-of-levels-in-bin ...

  5. leetcode树专题894.897,919,951

    满二叉树是一类二叉树,其中每个结点恰好有 0 或 2 个子结点. 返回包含 N 个结点的所有可能满二叉树的列表. 答案的每个元素都是一个可能树的根结点. 答案中每个树的每个结点都必须有 node.va ...

  6. leetcode 树的锯齿形状遍历

    二叉树的锯齿形层次遍历     给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如:给定二叉树 [3,9,20,null,n ...

  7. [leetcode] 树(Ⅱ)

    All questions are simple level. Construct String from Binary Tree Question[606]:You need to construc ...

  8. Leetcode 树 Populating Next Right Pointers in Each Node II

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...

  9. leetcode树相关

    目录 144前序遍历 94中序遍历(98验证二叉搜索树.230二叉搜索树中第K小的元素) 145后序遍历 102/107层次遍历(104二叉树最大深度.103 105从前序与中序遍历序列构造二叉树 1 ...

随机推荐

  1. 从输入URL到页面展示

    当我们输入 URL 并按回车后,浏览器会对 URL 进行检查,首先判断URL格式,比如是ftp http ed2k等等,我们这里假设这个URL是http://hellocassie.cn,那么浏览器会 ...

  2. ionic监听android返回键(实现“再按一次退出”功能)

    在android平台上的app,在主页面时经常会遇到"再按一次退出app"的功能,避免只按一下返回键就退出app提升体验优化. 1.这个功能需要我们用到ionic提供的regist ...

  3. 对JS中事件委托的理解

    什么是事件委托: 事件委托——给父元素绑定事件,用来监听子元素的冒泡事件,并找到是哪个子元素的事件.(不理解冒泡的可以去百度下) 定义:利用事件冒泡处理动态元素事件绑定的方法,专业术语叫事件委托. 使 ...

  4. 什么是data:image/png;base64,?一道关于Data URI Scheme的入门级CTF_Web题

    一道关于Data URI Scheme的入门级CTF_Web题 0x00 题目描述 这是偶尔遇到的某网安交流群的入群题,题目没有任何的提示,直接给了一个txt文件. 0x01 解题过程 通过给的这个文 ...

  5. 添加bash命令

    cd ~/.bash vim mya 键入 #!/bin/bash hostname -i :x 保存退出 source ~/.bash_profile 生效

  6. MySQL 整体架构一览

    MySQL 在整体架构上分为 Server 层和存储引擎层.其中 Server 层,包括连接器.查询缓存.分析器.优化器.执行器等,存储过程.触发器.视图和内置函数都在这层实现.数据引擎层负责数据的存 ...

  7. vue--基础应用 全选

    1.用computed实现全选 <body> <div id="app"> <input type="checkbox" v-mo ...

  8. Chrome调试工具常用功能

    一.打开的快捷键 windows: ctrl + shift + i/F12 1.Elements 1.选中 元素 切换至 Event… Tab可以查看这个元素绑定的事件 2.在 Element 选项 ...

  9. 每天都在用 Map,这些核心技术你知道吗?

    本篇文章站在多线程并发安全角度,带你了解多线程并发使用 HashMap 将会引发的问题,深入学习 ConcurrentHashMap ,带你彻底掌握这些核心技术. 全文摘要: HashMap 核心技术 ...

  10. 深度学习与人类语言处理-语音识别(part2)

    上节回顾深度学习与人类语言处理-语音识别(part1),这节课我们将学习如何将seq2seq模型用在语音识别 LAS 那我们来看看LAS的Encoder,Attend,Decoder分别是什么 Lis ...