【一棵树 内部求最值or求和】104. Maximum Depth of Binary Tree; 111. Minimum Depth of Binary Tree; 112. Path Sum; 226. Invert Binary Tree;

二叉树 Binary Tree

                -【一棵树 内部求最值or求和】

问题:

给定 层序遍历的数组 表示二叉树。

⚠️ 注意:leaf节点:既无左孩子,又无右孩子

  • 104:求二叉树的最深层数
  • Example:
    Given binary tree [3,9,20,null,null,15,7],
    
        3
       / \
      9  20
        /  \
       15   7
    return its depth = 3.
    
  • 111:求二叉树的最浅层数
  • Example:
    Given binary tree [3,9,20,null,null,15,7],
    
        3
       / \
      9  20
        /  \
       15   7
    return its minimum depth = 2.
    
  • 112:求二叉树的路径(root->leaf)是否存在和=sum的一条路径。
  • Example:
    Given the below binary tree and sum = 22,
    
          5
         / \
        4   8
       /   / \
      11  13  4
     /  \      \
    7    2      1
    return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

 

解法:Binary Tree (二叉树)

模版:一棵树问题:

 1 def solve(root)
 2     // 无效节点处理
 3     if not root: return...
 4     // 递归终点,base
 5     if f(root): return...
 6     // 左右子树,分类递归,讨论
 7     l = solve(root->left)
 8     r = solve(root->right)
 9     // 总结分类讨论
10     return g(l, r, root)
11 end

 

104:求二叉树的最深层数

代码参考:

 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 maxDepth(TreeNode* root) {
15         if(!root) return 0;
16         int l = maxDepth(root->left);
17         int r = maxDepth(root->right);
18         return max(l, r)+1;
19     }
20 };

 

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         if(!root->left && !root->right) return 1;
17         int l = minDepth(root->left);
18         if(!root->right) return l+1;
19         int r = minDepth(root->right);
20         if(!root->left) return r+1;
21         return min(l, r)+1;
22     }
23 };

⚠️ 注意:由于若出现只有单个孩子的情况,并不是leaf节点,因此,有 以下逻辑:

  • 若右子树为空,则返回左子树的结果。(L18,L19)
  • 若左子树为空,则返回右子树的结果。(L20,L21)

另外,

  • 递归的终点为leaf节点:无孩子节点的时候,返回 1 (L16)

 

112:求二叉树的路径(root->leaf)是否存在和=sum的一条路径。

代码参考:

 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     bool hasPathSum(TreeNode* root, int sum) {
15         if(!root) return false;
16         if(!root->left && !root->right) return sum==root->val;
17         bool l = hasPathSum(root->left, sum-root->val);
18         bool r = hasPathSum(root->right, sum-root->val);
19         return l || r;
20     }
21 };

⚠️ 注意:

以下两种结果不同,因此需要分开讨论:(L15 和 L16)

  • 空树 []                      ->  false
  • 只有root节点的树 [2]   ->  sum==2

 

226:将二叉树镜像翻转。

代码参考:

 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* invertTree(TreeNode* root) {
15         if(!root) return root;
16         
17         TreeNode* tmp = root->left;
18         root->left = root->right;
19         root->right = tmp;
20         
21         invertTree(root->left);
22         invertTree(root->right);
23         return root;
24     }
25 };

 

posted @ 2020-09-19 12:11  habibah_chang  阅读(135)  评论(0编辑  收藏  举报