代码随想录算法训练营第十五天| 110.平衡二叉树 (优先掌握递归) 257. 二叉树的所有路径 (优先掌握递归) 404.左叶子之和 (优先掌握递归)

 110.平衡二叉树 (优先掌握递归)

难点:

要求每个节点的左右字数的高度相减<=1,因此,需要对每个节点都进行检查,难就难在怎么获得任意节点的高度

其中递归是最简单的:

 

 1 int isB_cursor(TreeNode* node, bool &isBalance)
 2 {
 3     if (isBalance == false) return 0;
 4     if (!node) return 0;
 5     
 6     int leftLength = isB_cursor(node->left, isBalance);
 7     int rightLength = isB_cursor(node->right, isBalance);
 8 
 9     if (abs(leftLength - rightLength) > 1)
10     {
11         isBalance = false;
12     }
13 
14     return 1 + max(leftLength, rightLength);
15 }
16 bool isBalanced_cursor(TreeNode* root) {
17     bool result = true;
18     if (!root) return result;
19     isB_cursor(root, result);
20 
21     return result;
22 }

迭代法,很难

有两个方案获得任意节点的高度,一个是使用层序遍历

另一个很难,先弹出去当前节点,然后在押入,同时压进去左右孩子,类似一路到底,然后在回溯,回溯的时候-1,

用NULL来分开当前节点和左右孩子的分界线:

代码:

 1 int getMaxHeight(TreeNode* node)
 2 {
 3     if (!node) return 0;
 4 
 5     int result = 0;
 6     int depth = 0;
 7     stack<TreeNode*> selected;
 8     selected.push(node);
 9     while (!selected.empty())
10     {
11         TreeNode* cur_ = selected.top();
12         selected.pop();
13         if (cur_)
14         {
15             //先把自己压进去,然后压进去左右孩子,
16             selected.push(cur_);
17             selected.push(NULL);//代表是下一个了
18 
19             if (cur_->left) selected.push(cur_->left);
20             if (cur_->right)selected.push(cur_->right);
21 
22             depth++;
23         }
24         else
25         {
26             selected.pop();
27             depth--;
28         }
29         result = result > depth ? result : depth;
30     }
31 
32     return result;
33 }
34 
35 //先看每一个节点
36 bool isBalanced(TreeNode* root) {
37     bool result = true;
38     if (!root) return result;
39 
40     stack<TreeNode*> selected;
41     selected.push(root);
42 
43     while (!selected.empty())
44     {
45         auto cur_ = selected.top();
46         selected.pop();
47 
48         int leftLength = getMaxHeight(cur_->left);
49         int rightLength = getMaxHeight(cur_->right);
50 
51         if (abs(leftLength - rightLength) > 1)
52             return false;
53 
54         if (cur_->left) selected.push(cur_->left);
55         if (cur_->right) selected.push(cur_->right);
56     }
57 
58     return result;
59 }

 

 257. 二叉树的所有路径 (优先掌握递归)

递归:

 1 void binary_cur(TreeNode* node, vector<int>& path, vector<string>& result)
 2 {
 3     if (!node) return;
 4     path.push_back(node->val);
 5     if (!node->left && !node->right)
 6     {
 7         string str_ = to_string( path[0]);
 8         for (int i = 1; i < path.size(); i++)
 9         {
10             str_ += "->" + to_string(path[i]);
11         }
12 
13         result.push_back(str_);
14     }
15 
16     if (node->left) {
17         binary_cur(node->left, path, result);
18         path.pop_back();
19     }
20     if (node->right) {
21         binary_cur(node->right, path, result);
22         path.pop_back();
23     }
24 }
25 
26 vector<string> binaryTreePaths_(TreeNode* root) 
27 {
28     vector<string> result;
29     if (!root) return result;
30     vector<int> path;
31     binary_cur(root, path, result);
32 
33     return result;
34 }

 

迭代:

 1 vector<string> binaryTreePaths(TreeNode* root)
 2 {
 3     vector<string> result;
 4     if (!root) return result;
 5     stack<TreeNode*> selected;
 6     //写一个变量来存放已经遍历过的路径
 7     stack<string> paths;
 8     
 9     selected.push(root);
10     paths.push(to_string(root->val));
11 
12     while (!selected.empty()) 
13     {
14         auto cur_ = selected.top(); selected.pop();
15         auto path_ = paths.top(); paths.pop();
16 
17         if (!cur_->left && !cur_->right)
18         {
19             result.push_back(path_);
20         }
21 
22         if (cur_->left) {
23             selected.push(cur_->left);
24             paths.push(path_ + "->" + to_string(cur_->left->val));
25         }
26         if (cur_->right)
27         {
28             selected.push(cur_->right);
29             paths.push(path_ + "->" + to_string(cur_->right->val));
30         }
31     }
32 
33     return result;
34 }

 404.左叶子之和 (优先掌握递归)

迭代法:

 1 //迭代法:找出所有的左叶子
 2 int sumOfLeftLeaves(TreeNode* root) {
 3     int result = 0;
 4     if (!root) return result;
 5     stack<TreeNode*> selected;
 6 
 7     selected.push(root);
 8     while (!selected.empty())
 9     {
10         auto cur_ = selected.top(); selected.pop();
11 
12         if (cur_->left && !cur_->left->left && !cur_->left->right)
13             result += cur_->left->val;
14 
15         if (cur_->left) selected.push(cur_->left);
16         if (cur_->right) selected.push(cur_->right);
17     }
18 
19     return result;
20 }

递归法:

 1 int leftLeave_cur(TreeNode * node)
 2 {
 3     if (!node) return 0;
 4     if (!node->left && !node->right) return 0;
 5 
 6     int length = 0;
 7     if (node->left && !node->left->left && !node->left->right)
 8         length = node->left->val;
 9 
10     int left_ = 0;
11     if (node->left) left_ = leftLeave_cur(node->left);
12     int right_ = 0;
13     if (node->right) right_ = leftLeave_cur(node->right);
14     
15     return length+ left_ + right_;
16 }
17 int sumOfLeftLeaves_cursor(TreeNode* root) {
18     int result = 0;
19     if (!root) return result;
20 
21     result = leftLeave_cur(root);
22 
23     return result;
24 }

 

posted @ 2023-06-23 15:45  博二爷  阅读(4)  评论(0编辑  收藏  举报