DAY 15 二叉樹part03

xzdmzrc發表於2024-08-01

110.平衡二叉樹 (優先掌握遞迴)

題目連結/文章講解/影片講解:https://programmercarl.com/0110.%E5%B9%B3%E8%A1%A1%E4%BA%8C%E5%8F%89%E6%A0%91.html

獨立完成,感覺還比較好理解

12 class Solution {
13 public:
14     bool isBalanced(TreeNode* root) {
15         if(root==nullptr) return true;
16         if(abs(gethigh(root->right)-gethigh(root->left))>1) return false;
17         else return isBalanced(root->right)&&isBalanced(root->left);
18     }
19     int gethigh(TreeNode*root)
20     {
21         int high=0;
22         if(root==nullptr) return high;
23         int righth=gethigh(root->right);
24         int lefth=gethigh(root->left);
25         high=1+max(righth,lefth);
26         return high;
27     }
28 };

257. 二叉樹的所有路徑 (優先掌握遞迴)

題目連結/文章講解/影片講解:https://programmercarl.com/0257.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%89%80%E6%9C%89%E8%B7%AF%E5%BE%84.html

12 class Solution {
13 public:
14     vector<string> binaryTreePaths(TreeNode* root) {
15         vector<string> result;
16         vector<vector<int>> path;
17         vector<int> res;
18         if(root==nullptr) return result;
19         seekpath(root,res,path);
20         for(int i=0;i<path.size();i++)
21         {
22             string tmp;
23             for(int j=0;j<path[i].size();j++)
24             {
25                 tmp+=to_string(path[i][j]);
26                 if(j!=path[i].size()-1) tmp+="->";
27             }
28             result.push_back(tmp);
29        }
30 
31        return result;
32     }
33     void seekpath(TreeNode* root,vector<int> res, vector<vector<int>>& path)
34     {
35         res.push_back(root->val);
36         if(root->right==nullptr&&root->left==nullptr) 
37         {
38             path.push_back(res);
39             return ;
40         }
41         if(root->right) seekpath(root->right,res,path);
42         if(root->left) seekpath(root->left,res,path);
43         return ;
44 
45     }
46 };

邏輯沒問題但是字串轉換和輸出寫複雜了

404.左葉子之和 (優先掌握遞迴)

題目連結/文章講解/影片講解:https://programmercarl.com/0404.%E5%B7%A6%E5%8F%B6%E5%AD%90%E4%B9%8B%E5%92%8C.html

12 class Solution {
13 public:
14     int sumOfLeftLeaves(TreeNode* root) {
15         int sum=0;
16         if(root==nullptr) return 0;
17         getsum(root,sum);
18         return sum;
19     }
20     void getsum(TreeNode* root,int &sum)
21     {
22         if(root->left&&!root->left->left&&!root->left->right) 
23             sum+=root->left->val;
24         if(root->left) getsum(root->left,sum);
25         if(root->right) getsum(root->right,sum);
26         return;
27         
28 
29     }
30 };

沒什麼難度

精簡版:

 1 class Solution {
 2 public:
 3     int sumOfLeftLeaves(TreeNode* root) {
 4         if (root == NULL) return 0;
 5         int leftValue = 0;
 6         if (root->left != NULL && root->left->left == NULL && root->left->right == NULL) {
 7             leftValue = root->left->val;
 8         }
 9         return leftValue + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
10     }
11 };

222.完全二叉樹的節點個數(優先掌握遞迴)

題目連結/文章講解/影片講解:https://programmercarl.com/0222.%E5%AE%8C%E5%85%A8%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E8%8A%82%E7%82%B9%E4%B8%AA%E6%95%B0.html

1 class Solution {
2 public:
3     int countNodes(TreeNode* root) {
4         if (root == NULL) return 0;
5         return 1 + countNodes(root->left) + countNodes(root->right);
6     }
7 };

相關文章