代码随想录算法训练营第二十天| 669. 修剪二叉搜索树 108.将有序数组转换为二叉搜索树 538.把二叉搜索树转换为累加树

669. 修剪二叉搜索树

思路

递归法: 

需要思考清楚,如果当前节点<low,那么就返回递归它的右节点,而不是自己取判断,找出来一个合适的节点,这样的话会越想越乱

代码:

 1 TreeNode* trimBST_cursor(TreeNode* root, int low, int high) {
 2     if (!root) return nullptr;
 3 
 4     if (root->val < low)
 5     {
 6         return trimBST_cursor(root->right, low, high);
 7     }
 8 
 9     if (root->val > high)
10     {
11         return trimBST_cursor(root->left, low, high);
12     }
13 
14     if (root->left)
15     {
16         root->left = trimBST_cursor(root->left, low, high);
17     }
18 
19     if (root->right)
20     {
21         root->right = trimBST_cursor(root->right, low, high);
22     }
23 
24     return root;
25 }

迭代法:

需要先把当前的root,放到符合标准的区间里,这样不管左子树的右孩子,再怎么大,也不会大过root,所以他也就不会超过high

代码

 1 TreeNode* trimBST(TreeNode* root, int low, int high) {
 2     if (!root) return nullptr;
 3 
 4     //当前root位于最近的[]范围里
 5     while (root&&(root->val<low||root->val>high))
 6     {
 7         if (root->val < low)
 8             root = root->right;
 9         else if (root->val > high)
10             root = root->left;
11     }
12 
13     //让cur = root,开始向左遍历,如果左孩子小于low,那么就一直遍历它的右孩子,直到它的右孩子>low
14     auto curNode = root;
15     while (curNode)
16     {
17         while (curNode->left && curNode->left->val < low)
18         {
19             curNode->left = curNode->left->right;
20         }
21         //遇到一个左孩子小于的话,那么就把他删掉
22         curNode = curNode->left;
23     }
24 
25     curNode = root;
26     while (curNode)
27     {
28         while (curNode->right && curNode->right->val > high)
29         {
30             curNode->right = curNode->right->left;
31         }
32 
33         curNode = curNode->right;
34     }
35 
36     return root;
37 }

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

比较简单,直接上代码:

 1 TreeNode* sortedArrayToBST(vector<int>& nums)
 2 {
 3     if (nums.empty()) return nullptr;
 4 
 5     int midIndex = nums.size() / 2;
 6 
 7     TreeNode* root = new TreeNode(nums[midIndex]);
 8 
 9     vector<int> left = vector<int>(nums.begin(), nums.begin() + midIndex);
10 
11     vector<int>right = midIndex + 1 < nums.size() ? vector<int>(nums.begin() + midIndex + 1, nums.end()) : vector<int>{};
12 
13     root->left = sortedArrayToBST(left);
14     root->right = sortedArrayToBST(right);
15 
16     return root;
17 }

 

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

思路:

1,中序遍历,然后找到所有的节点,然后从后到前的更改数值

2,定义一个pre,来存储前一个的数值

代码:

 1 void convertBST_cur(TreeNode* root, vector<TreeNode*>& nodes)
 2 {
 3     if (!root) return ;
 4     if (root->left) convertBST_cur(root->left, nodes);
 5     nodes.push_back(root);
 6     if (root->right) convertBST_cur(root->right, nodes);
 7 }
 8 TreeNode* convertBST(TreeNode* root) {
 9     if (!root) return nullptr;
10 
11     stack<TreeNode*> selected;
12     selected.push(root);
13     vector<TreeNode*> nodes;
14     convertBST_cur(root, nodes);
15     
16     //如果更改它们的值,那么就会导致链接出现了问题
17     for (int i = nodes.size() - 2; i >= 0; i--)
18     {
19         nodes[i]->val += nodes[i + 1]->val;
20     }
21 
22     return root;
23 }

 

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