一级类目 二级类型/题目 三级类目/题目

二叉树的遍历 144. 二叉树的前序遍历
94. 二叉树的中序遍历
145. 二叉树的后序遍历
剑指 Offer 32 - I. 从上到下打印二叉树
剑指 Offer 32 - II. 从上到下打印二叉树 II
剑指 Offer 32 - III. 从上到下打印二叉树 III
107. 二叉树的层序遍历 II
面试题34. 二叉树中和为某一值的路径
构造二叉树

105. 从前序与中序遍历序列构造二叉树

| | | | |

106. 从中序与后序遍历序列构造二叉树

| | | | |

889. 根据前序和后序遍历构造二叉树

| | | | |

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

| | | | |

109. 有序链表转换二叉搜索树

| | | | |

114. 二叉树展开为链表

| | | | |

617. 合并二叉树

| | | | 输出二叉树 |

剑指 Offer 37. 序列化二叉树

| | | |



二叉树的验证 |

100. 相同的树

| | | | |

剑指 Offer 28. 对称的二叉树

| | | | |

剑指 Offer 55 - II. 平衡二叉树

| | | | |

98. 验证二叉搜索树

| | | | |

958. 二叉树的完全性检验

| | | | |

剑指 Offer 26. 树的子结构

| | | | |

572. 另一棵树的子树

| | | | |

剑指 Offer 27. 二叉树的镜像

| | | | | 156.上下翻转二叉树 | |

二叉树

二叉树的遍历

144. 二叉树的前序遍历

  • 解法一:递归

    1. class Solution {
    2. public:
    3. vector<int> res;
    4. void preOrder(TreeNode *root) {
    5. if (!root)return;
    6. res.push_back(root->val);
    7. preOrder(root->left);
    8. preOrder(root->right);
    9. }
    10. vector<int> preorderTraversal(TreeNode *root) {
    11. preOrder(root);
    12. return res;
    13. }
    14. };
  • 解法二:使用栈迭代

image.png

  1. class Solution {
  2. public:
  3. vector<int> res;
  4. // root ,left,right
  5. vector<int> preorderTraversal(TreeNode *root) {
  6. if (!root)return res;
  7. stack<TreeNode *> st;
  8. TreeNode *node = root;
  9. while (!st.empty() || node) {
  10. while (node) {
  11. res.emplace_back(node->val);
  12. st.emplace(node);
  13. node = node->left;
  14. }
  15. node = st.top();
  16. st.pop();
  17. node = node->right;
  18. }
  19. return res;
  20. }
  21. };

94. 二叉树的中序遍历

  • 解法一

    1. class Solution {
    2. public:
    3. vector<int> res;
    4. void inorder(TreeNode *root) {
    5. if (!root)return;
    6. inorder(root->left);
    7. res.push_back(root->val);
    8. inorder(root->right);
    9. }
    10. vector<int> inorderTraversal(TreeNode *root) {
    11. inorder(root);
    12. return res;
    13. }
    14. };
  • 解法二

    1. class Solution {
    2. public:
    3. vector<int> res;
    4. // root ,left,right
    5. vector<int> inorderTraversal(TreeNode *root) {
    6. if (!root)return res;
    7. stack<TreeNode *> stack;
    8. while (root || !stack.empty()) {
    9. while (root) {
    10. stack.emplace(root);
    11. root = root->left;
    12. }
    13. root = stack.top();
    14. stack.pop();
    15. res.emplace_back(root->val);
    16. root = root->right;
    17. }
    18. return res;
    19. }
    20. };

    145. 二叉树的后序遍历

  • 解法一:

    1. class Solution {
    2. public:
    3. vector<int> res;
    4. void postOrder(TreeNode *root) {
    5. if (!root)return;
    6. postOrder(root->left);
    7. postOrder(root->right);
    8. res.emplace_back(root->val);
    9. }
    10. //left right root
    11. vector<int> postorderTraversal(TreeNode *root) {
    12. postOrder(root);
    13. return res;
    14. }
    15. };

    剑指 Offer 32 - I. 从上到下打印二叉树

    ```cpp class Solution { public: vector res; vector levelOrder(TreeNode *root) {

    1. if (root == nullptr)
    2. {
    3. return res;
    4. }
    5. queue<TreeNode *> queue;
    6. queue.push(root);
    7. while (!queue.empty())
    8. {
    9. int size = queue.size();
    10. for (int i = 0; i < size; i++)
    11. {
    12. TreeNode *tmp = queue.front();
    13. res.push_back(tmp->val);
    14. queue.pop();
    15. if (tmp->left)
    16. {
    17. queue.push(tmp->left);
    18. }
    19. if (tmp->right)
    20. {
    21. queue.push(tmp->right);
    22. }
    23. }
    24. }
    25. return res;

    } };

  1. <a name="aDF0i"></a>
  2. #### [剑指 Offer 32 - II. 从上到下打印二叉树 II](https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/)
  3. ```cpp
  4. /**
  5. * Definition for a binary tree node.
  6. * struct TreeNode {
  7. * int val;
  8. * TreeNode *left;
  9. * TreeNode *right;
  10. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  11. * };
  12. */
  13. class Solution
  14. {
  15. public:
  16. vector<vector<int>> res;
  17. vector<vector<int>> levelOrder(TreeNode *root)
  18. {
  19. if (root == nullptr)
  20. {
  21. return res;
  22. }
  23. queue<TreeNode *> queue;
  24. queue.push(root);
  25. while (!queue.empty())
  26. {
  27. int size = queue.size();
  28. vector<int> ans;
  29. for (int i = 0; i < size; i++)
  30. {
  31. TreeNode *tmp = queue.front();
  32. ans.push_back(tmp->val);
  33. queue.pop();
  34. if (i == size - 1)
  35. {
  36. res.push_back(ans);
  37. }
  38. if (tmp->left)
  39. {
  40. queue.push(tmp->left);
  41. }
  42. if (tmp->right)
  43. {
  44. queue.push(tmp->right);
  45. }
  46. }
  47. }
  48. return res;
  49. }
  50. };

剑指 Offer 32 - III. 从上到下打印二叉树 III

  1. class Solution
  2. {
  3. public:
  4. vector<vector<int>> res;
  5. vector<vector<int>> levelOrder(TreeNode *root)
  6. {
  7. if (!root)
  8. {
  9. return res;
  10. }
  11. queue<TreeNode *> queue;
  12. queue.push(root);
  13. int count = 1;
  14. while (!queue.empty())
  15. {
  16. int size = queue.size();
  17. vector<int> ans;
  18. for (int i = 0; i < size; i++)
  19. {
  20. TreeNode *node = queue.front();
  21. ans.push_back(node->val);
  22. queue.pop();
  23. if (i == size - 1)
  24. {
  25. if (!(count & 1))
  26. {
  27. reverse(ans.begin(), ans.end());
  28. }
  29. res.push_back(ans);
  30. }
  31. if (node->left)
  32. {
  33. queue.push(node->left);
  34. }
  35. if (node->right)
  36. {
  37. queue.push(node->right);
  38. }
  39. }
  40. count += 1;
  41. }
  42. return res;
  43. }
  44. };

107. 二叉树的层序遍历 II

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution
  13. {
  14. public:
  15. vector<vector<int>> res;
  16. vector<vector<int>> levelOrderBottom(TreeNode *root)
  17. {
  18. if (!root)
  19. {
  20. return res;
  21. }
  22. queue<TreeNode *> queue;
  23. queue.push(root);
  24. while (!queue.empty())
  25. {
  26. int size = queue.size();
  27. vector<int> ans;
  28. for (int i = 0; i < size; i++)
  29. {
  30. TreeNode *node = queue.front();
  31. ans.push_back(node->val);
  32. queue.pop();
  33. if (i == size - 1)
  34. {
  35. res.push_back(ans);
  36. }
  37. if (node->left)
  38. {
  39. queue.push(node->left);
  40. }
  41. if (node->right)
  42. {
  43. queue.push(node->right);
  44. }
  45. }
  46. }
  47. reverse(res.begin(),res.end());
  48. return res;
  49. }
  50. };

面试题34. 二叉树中和为某一值的路径

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14. vector<vector<int>> res;
  15. vector<int> path;
  16. vector<vector<int>> pathSum(TreeNode* root, int target) {
  17. dfs(root,target);
  18. return res;
  19. }
  20. void dfs(TreeNode* root,int target){
  21. if(root==nullptr){
  22. return;
  23. }
  24. target-=root->val;
  25. path.push_back(root->val);
  26. if(target==0&&root->left==nullptr&&root->right==nullptr){
  27. res.push_back(path);
  28. }else {
  29. dfs(root->left,target);
  30. dfs(root->right,target);
  31. }
  32. path.pop_back();
  33. }
  34. };

构造二叉树

105. 从前序与中序遍历序列构造二叉树

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14. unordered_map<int, int> index;
  15. TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
  16. int n = inorder.size();
  17. for (int i = 0; i < n; i++) {
  18. index[inorder[i]] = i;
  19. }
  20. return myselfBuild(preorder, inorder, 0, n - 1, 0, n - 1);
  21. }
  22. // 根 [左子树遍历结果] [右子树遍历结果]
  23. // [左子树遍历结果] 根 [右子树遍历结果]
  24. TreeNode *myselfBuild(vector<int> &preorder, vector<int> &inorder, int preorder_left,
  25. int preorder_right, int inorder_left, int inorder_right) {
  26. if (preorder_left > preorder_right) {
  27. return nullptr;
  28. }
  29. int inorder_root = index[preorder[preorder_left]];
  30. TreeNode *root = new TreeNode(inorder[inorder_root]);
  31. int size_left_subtree = inorder_root - inorder_left;
  32. root->left = myselfBuild(preorder,
  33. inorder,
  34. preorder_left + 1,
  35. preorder_left + size_left_subtree,
  36. inorder_left, inorder_root - 1);
  37. root->right = myselfBuild(preorder,
  38. inorder,
  39. preorder_left + size_left_subtree + 1,
  40. preorder_right,
  41. inorder_root + 1,
  42. inorder_right
  43. );
  44. return root;
  45. }
  46. };

106. 从中序与后序遍历序列构造二叉树

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14. unordered_map<int, int> index;
  15. // 不要像105那样将数组也递归 ,容易stackoverflow ,使用一个全局代替;
  16. vector<int> post;
  17. TreeNode *buildMySelf(int postorder_left, int postorder_right,
  18. int inorder_left, int inorder_right) {
  19. if (postorder_left > postorder_right || inorder_left > inorder_right) {
  20. return nullptr;
  21. }
  22. int root_val = post[postorder_right];
  23. int inorder_root = index[root_val];
  24. TreeNode *root = new TreeNode(root_val);
  25. int size_left_tree = inorder_root - inorder_left;
  26. root->left = buildMySelf(
  27. postorder_left,
  28. postorder_left + size_left_tree - 1,
  29. inorder_left,
  30. inorder_root - 1);
  31. root->right = buildMySelf(
  32. postorder_left + size_left_tree,
  33. postorder_right - 1,
  34. inorder_root + 1,
  35. inorder_right);
  36. return root;
  37. }
  38. // 左子树遍历 右子树遍历 根
  39. // 左子树遍历 根 右子树遍历
  40. TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
  41. int n = inorder.size();
  42. for (int i = 0; i < n; i++) {
  43. index[inorder[i]] = i;
  44. }
  45. post=postorder;
  46. return buildMySelf(0, n - 1, 0, n - 1);
  47. }
  48. };

889. 根据前序和后序遍历构造二叉树

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14. // 根 左子树遍历 右子树遍历
  15. // 左子树遍历 右子树遍历 根
  16. // 1,2,4,5,3,6,7
  17. // 4,5,2,6,7,3,1
  18. unordered_map<int, int> postorder_index;
  19. vector<int> preorderArr;
  20. TreeNode *constructFromPrePost(vector<int> &preorder, vector<int> &postorder) {
  21. int n = preorder.size();
  22. for (int i = 0; i < n; i++) {
  23. postorder_index[postorder[i]] = i;
  24. }
  25. preorderArr = preorder;
  26. return buildSubTree(0, n - 1, 0, n - 1);
  27. }
  28. TreeNode *buildSubTree(
  29. int preorder_left, int preorder_right,
  30. int postorder_left, int postorder_right) {
  31. if (preorder_left > preorder_right) { return nullptr; }
  32. TreeNode *root = new TreeNode(preorderArr[preorder_left]);
  33. if (preorder_left == preorder_right) { return root; }
  34. // 先序遍历 第二个元素就是左子树 的根节点,也就是2
  35. // 找到2在后序遍历中的位置 ,后续遍历中2之前全部是左子树部分,这样就可以找到左子树长度;
  36. int post_left_index = postorder_index[preorderArr[preorder_left + 1]];
  37. //左子树长度;
  38. int size_left_subtree = post_left_index - postorder_left + 1;
  39. // 1,2,4,5,3,6,7
  40. // 4,5,2,6,7,3,1
  41. root->left = buildSubTree(
  42. preorder_left + 1,
  43. preorder_left + size_left_subtree,
  44. postorder_left, post_left_index
  45. );
  46. root->right = buildSubTree(
  47. preorder_left + size_left_subtree + 1,
  48. preorder_right,
  49. post_left_index + 1,
  50. postorder_right - 1
  51. );
  52. return root;
  53. }
  54. };

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

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14. TreeNode *sortedArrayToBST(vector<int> &nums) {
  15. if (nums.empty()) {
  16. return nullptr;
  17. }
  18. return buildBinaryTree(nums, 0, nums.size() - 1);
  19. }
  20. TreeNode *buildBinaryTree(vector<int> &nums, int left, int right) {
  21. if (left > right) {
  22. return nullptr;
  23. }
  24. int mid = left + ((right - left) >> 1);
  25. TreeNode *root = new TreeNode(nums[mid]);
  26. root->left = buildBinaryTree(nums, left, mid - 1);
  27. root->right = buildBinaryTree(nums, mid + 1, right);
  28. return root;
  29. }
  30. };

109. 有序链表转换二叉搜索树

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode() : val(0), next(nullptr) {}
  7. * ListNode(int x) : val(x), next(nullptr) {}
  8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. /**
  12. * Definition for a binary tree node.
  13. * struct TreeNode {
  14. * int val;
  15. * TreeNode *left;
  16. * TreeNode *right;
  17. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  18. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  19. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  20. * };
  21. */
  22. class Solution {
  23. public:
  24. ListNode *getMidNode(ListNode *left, ListNode *right) {
  25. ListNode *fast = left;
  26. ListNode *slow = left;
  27. while (fast != right && fast->next!= right) {
  28. fast = fast->next->next;
  29. slow = slow->next;
  30. }
  31. return slow;
  32. }
  33. TreeNode *buildSubTree(ListNode *left, ListNode *right) {
  34. if (left == right) {
  35. return nullptr;
  36. }
  37. ListNode *mid = getMidNode(left, right);
  38. TreeNode *root = new TreeNode(mid->val);
  39. root->left = buildSubTree(left, mid);
  40. root->right = buildSubTree(mid->next, right);
  41. return root;
  42. }
  43. TreeNode *sortedListToBST(ListNode *head) {
  44. if (head == nullptr) {
  45. return nullptr;
  46. }
  47. return buildSubTree(head, nullptr);
  48. }
  49. };

114. 二叉树展开为链表

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14. void flatten(TreeNode *root) {
  15. while(root){
  16. if(!root->left){
  17. root=root->right;
  18. }else {
  19. TreeNode *prev=root->left;
  20. while(prev->right){
  21. prev=prev->right;
  22. }
  23. prev->right=root->right; //将右子树嫁接过来
  24. root->right=root->left;
  25. root->left=nullptr;
  26. root=root->right;
  27. }
  28. }
  29. }
  30. };

617. 合并二叉树

  • 在原来的一个树上做合并操作

    1. TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
    2. if(!root1){
    3. return root2;
    4. }
    5. if(!root2){return root1;}
    6. root1->val+=root2->val;
    7. root1->left=mergeTrees(root1->left,root2->left);
    8. root1->right=mergeTrees(root1->right,root2->right);
    9. return root1;
    10. }
  • 在一棵新树上做合并操作

    1. TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
    2. if(!root1){
    3. return root2;
    4. }
    5. if(!root2){return root1;}
    6. TreeNode* tmp=new TreeNode(root1->val+root2->val);
    7. tmp->left=mergeTrees(root1->left,root2->left);
    8. tmp->right=mergeTrees(root1->right,root2->right);
    9. return tmp;
    10. }

输出二叉树

剑指 Offer 37. 序列化二叉树

二叉树的验证

100. 相同的树

  1. bool isSameTree(TreeNode *p, TreeNode *q) {
  2. // 两个要么都是null ,如果有一个是null 则是不相同的;
  3. if ((p && !q) || (!p && q)) {
  4. return false;
  5. }
  6. if (p == q && !q) {
  7. return true;
  8. }
  9. return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
  10. }

剑指 Offer 28. 对称的二叉树

  1. bool isSymmetric(TreeNode *root) {
  2. return !root ? true : recur(root->left, root->right);
  3. }
  4. bool recur(TreeNode *left, TreeNode *right) {
  5. if (!left && !right) { return true; }
  6. if (!left || !right || left->val != right->val) {
  7. return false;
  8. }
  9. return recur(left->left, right->right) && recur(left->right, right->left);
  10. }

剑指 Offer 55 - II. 平衡二叉树

  1. int depth(TreeNode *root) {
  2. if (!root) {
  3. return 0;
  4. }
  5. int left = depth(root->left);
  6. int right = depth(root->right);
  7. return max(left, right) + 1;
  8. }
  9. bool isBalanced(TreeNode *root) {
  10. if (!root) {
  11. return true;
  12. }
  13. return abs(depth(root->left) - depth(root->right)) < 2
  14. && isBalanced(root->left)
  15. && isBalanced(root->right);
  16. }

98. 验证二叉搜索树

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14. vector<int> res;
  15. bool isValidBST(TreeNode *root) {
  16. inOrder(root);
  17. for (int i = 1; i < res.size(); i++) {
  18. if (res[i] <= res[i - 1]) {
  19. return false;
  20. }
  21. }
  22. return true;
  23. }
  24. void inOrder(TreeNode *root) {
  25. if (!root) {
  26. return;
  27. }
  28. inOrder(root->left);
  29. res.push_back(root->val);
  30. inOrder(root->right);
  31. }
  32. };

958. 二叉树的完全性检验

  1. bool isCompleteTree(TreeNode *root) {
  2. queue<TreeNode *> q;
  3. q.push(root);
  4. bool hasNull = false;
  5. while (!q.empty()) {
  6. TreeNode *curr = q.front();
  7. q.pop();
  8. if (curr == nullptr) {
  9. hasNull = true;
  10. continue;
  11. } else {
  12. if (hasNull) {
  13. return false;
  14. }
  15. q.push(curr->left);
  16. q.push(curr->right);
  17. }
  18. }
  19. return true;
  20. }

993. 二叉树的堂兄弟节点

层序遍历时判断父亲节点个数

  1. bool isCousins(TreeNode *root, int x, int y) {
  2. queue<pair<TreeNode *, TreeNode *>> q;
  3. q.push({root, nullptr});
  4. while (!q.empty()) {
  5. int n = q.size();
  6. vector<TreeNode *> cur_parent;
  7. for (int i = 0; i < n; i++) {
  8. auto[cur, parent] = q.front();
  9. q.pop();
  10. if (cur->val == x || cur->val == y) {
  11. cur_parent.push_back(parent);
  12. }
  13. if (cur->left) {
  14. q.push({cur->left, cur});
  15. }
  16. if (cur->right) {
  17. q.push({cur->right, cur});
  18. }
  19. }
  20. if (cur_parent.size() == 0) {
  21. continue;
  22. }
  23. if (cur_parent.size() == 1) {
  24. return false;
  25. }
  26. if (cur_parent.size() == 2) {
  27. return cur_parent[0] != cur_parent[1];
  28. }
  29. }
  30. return false;
  31. }

剑指 Offer 26. 树的子结构

  1. bool isSubStructure(TreeNode *A, TreeNode *B) {
  2. if (!A || !B) {
  3. return false;
  4. }
  5. return (recur(A, B) || isSubStructure(A->left, B) || isSubStructure(A->right, B));
  6. bool recur(TreeNode *A, TreeNode *B) {
  7. if (!B) { return true; }
  8. if (!A || A->val != B->val) {
  9. return false;
  10. }
  11. return recur(A->left, B->left) && recur(A->right, B->right);
  12. }

572. 另一棵树的子树

  1. bool isSubtree(TreeNode *A, TreeNode *B) {
  2. //两棵树 任一一棵都不允许为空
  3. if (!A) {
  4. return false;
  5. }
  6. if (isSameTree(A, B)) {
  7. return true;
  8. }
  9. return isSubtree(A->left, B) || isSubtree(A->right, B);
  10. }
  11. bool isSameTree(TreeNode *q, TreeNode *p) {
  12. if ((!q && p) || (!p && q)) {
  13. return false;
  14. }
  15. if (p == q && !q) {
  16. return true;
  17. }
  18. return q->val == p->val && isSameTree(q->left, p->left) && isSameTree(p->right, q->right);
  19. }

二叉树翻转

剑指 Offer 27. 二叉树的镜像

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. TreeNode* mirrorTree(TreeNode* root) {
  13. // write code here
  14. if (root == nullptr) {
  15. return nullptr;
  16. }
  17. TreeNode *tmp = root->left;
  18. root->left = root->right;
  19. root->right = tmp;
  20. mirrorTree(root->right);
  21. mirrorTree(root->left);
  22. return root;
  23. }
  24. };

156.上下翻转二叉树

  1. class Solution {
  2. public:
  3. TreeNode *head;
  4. TreeNode upsideDownBinaryTree(TreeNode *root){
  5. if (!root){
  6. return root;
  7. }
  8. dfs(root);
  9. return head;
  10. }
  11. TreeNode *dfs(TreeNode *root){
  12. if (!root){
  13. return false;
  14. }
  15. //对于任意一个节点,它的右节点变成左子结点的左子结点,它自己变成它左子结点的右子节点。
  16. if (!root->left && !root->right){
  17. if (!head){
  18. head = root;
  19. }
  20. return root;
  21. }
  22. TreeNode* left=dfs(root->left);
  23. TreeNode* right=dfs(root->right);
  24. if(!left){
  25. left->left=right;
  26. left->right=root;
  27. }
  28. root->left=nullptr;
  29. root->right=nullptr;
  30. return root;
  31. }

二叉树的深度和直径

剑指 Offer 55 - I. 二叉树的深度

  1. class Solution {
  2. public:
  3. int maxDepth(TreeNode* root) {
  4. if(!root){
  5. return 0;
  6. }
  7. int left=maxDepth(root->left);
  8. int right=maxDepth(root->right);
  9. return max(left,right)+1;
  10. }
  11. };

111. 二叉树的最小深度

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14. int minDepth(TreeNode *root) {
  15. if (!root) { return 0; }
  16. queue<pair<TreeNode *, int>> q;
  17. q.push({root, 1});
  18. while (!q.empty()) {
  19. auto t = q.front();
  20. q.pop();
  21. TreeNode *now = t.first;
  22. int nowDepth = t.second;
  23. if (!now->left && !now->right) {
  24. return nowDepth;
  25. }
  26. if (now->left) {
  27. q.push({now->left, nowDepth + 1});
  28. }
  29. if (now->right) {
  30. q.push({now->right, nowDepth + 1});
  31. }
  32. }
  33. return -1;
  34. }
  35. };


543. 二叉树的直径

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14. int maxd = 0;
  15. int diameterOfBinaryTree(TreeNode *root) {
  16. depth(root);
  17. return maxd;
  18. }
  19. /**
  20. * 遍历 左右 最深的同时维护一个 maxd
  21. * @param root
  22. * @return
  23. */
  24. int depth(TreeNode *root) {
  25. if (!root) {
  26. return 0;
  27. }
  28. int left = depth(root->left);
  29. int right = depth(root->right);
  30. maxd = max(left + right, maxd);
  31. return max(left, right) + 1;
  32. }
  33. };

257. 二叉树的所有路径

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10. * };
  11. */
  12. class Solution {
  13. public:
  14. vector<string> res;
  15. string path;
  16. vector<string> binaryTreePaths(TreeNode* root) {
  17. dfs(root,"");
  18. return res;
  19. }
  20. void dfs(TreeNode* root,string path){
  21. if(!root){
  22. return;
  23. }
  24. if(!root->left && !root->right){
  25. path+=to_string(root->val);
  26. res.push_back(path);
  27. return;
  28. }
  29. path+=to_string(root->val)+"->";
  30. dfs(root->left,path);
  31. dfs(root->right,path);
  32. }
  33. };

112. 路径总和

  1. class Solution {
  2. public:
  3. bool hasPathSum(TreeNode* root, int targetSum) {
  4. return dfs(root,0,targetSum);
  5. }
  6. bool dfs(TreeNode* root,int tmpSum,int targetSum){
  7. if(!root){return false;}
  8. tmpSum+=root->val;
  9. if(!root->left && !root->right){
  10. return tmpSum==targetSum;
  11. }else {
  12. return dfs(root->left,tmpSum,targetSum)
  13. || dfs(root->right,tmpSum,targetSum);
  14. }
  15. }
  16. };