当前位置:   article > 正文

二刷代码随想录训练营Day 21|669. 修剪二叉搜索树、108.将有序数组转换为二叉搜索树、538.把二叉搜索树转换为累加树、总结篇

二刷代码随想录训练营Day 21|669. 修剪二叉搜索树、108.将有序数组转换为二叉搜索树、538.把二叉搜索树转换为累加树、总结篇

1.修建二叉搜索树

代码:递归法

  1. class Solution {
  2. public:
  3. TreeNode* trimBST(TreeNode* root, int low, int high) {
  4. if(root == NULL) return NULL;
  5. // 中
  6. if(root->val > high){
  7. return trimBST(root->left,low,high);
  8. }
  9. if(root->val < low){
  10. return trimBST(root->right,low,high);
  11. }
  12. // 左
  13. root->left = trimBST(root->left,low,high);
  14. // 右
  15. root->right = trimBST(root->right,low,high);
  16. return root;
  17. }
  18. };

 note:这里的易错点是,当我们遇到要删除的结点时,会根据情况舍弃它的左子树或右子树,但是还是要递归调用函数来去修剪我们留下来的那颗子树。最后不要忘了更新root的新的左右子树。

代码:迭代法

  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* trimBST(TreeNode* root, int low, int high) {
  15. if(!root) return nullptr;
  16. while(root != nullptr && (root->val < low || root->val > high)){
  17. if(root->val < low) root = root->right;
  18. else root = root->left;
  19. }
  20. TreeNode* cur = root;
  21. while(cur != nullptr){
  22. while(cur->left && cur->left->val < low){
  23. cur->left = cur->left->right;
  24. }
  25. cur = cur->left;
  26. }
  27. cur = root;
  28. while(cur != nullptr){
  29. while(cur->right && cur->right->val > high){
  30. cur->right = cur->right->left;
  31. }
  32. cur = cur->right;
  33. }
  34. return root;
  35. }
  36. };

note:迭代法其实就是不断地剪枝。二叉搜索树的迭代法都不需要用到栈。

2.将有序数组转化为二叉搜索树

代码:递归法

  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. private: // 左闭右闭
  14. TreeNode* traversal(vector<int> &nums,int left,int right){
  15. // 递归出口
  16. if(left > right) return nullptr;
  17. int mid = left + (right - left) / 2;
  18. // 中
  19. TreeNode* node = new TreeNode(nums[mid]);
  20. // 左
  21. node->left = traversal(nums,left,mid - 1);
  22. // 右
  23. node->right = traversal(nums,mid + 1,right);
  24. return node;
  25. }
  26. public:
  27. TreeNode* sortedArrayToBST(vector<int>& nums) {
  28. return traversal(nums,0,nums.size() - 1);
  29. }
  30. };

 note:注意这里左右递归的时候的参数 是left mid -1 和 mid + 1 right

3.将二叉搜索树转变为累加树

代码:递归法

  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. private:
  14. int pre = 0;
  15. void traversal(TreeNode* node){
  16. if(node == NULL) return;
  17. // 右
  18. traversal(node->right);
  19. // 中
  20. node->val += pre;
  21. pre = node->val;
  22. // 左
  23. traversal(node->left);
  24. }
  25. public:
  26. TreeNode* convertBST(TreeNode* root) {
  27. traversal(root);
  28. return root;
  29. }
  30. };

 代码:迭代法

  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* convertBST(TreeNode* root) {
  15. stack<TreeNode*>st;
  16. if(root == NULL) return NULL;
  17. int pre = 0;
  18. st.push(root);
  19. while(!st.empty()){
  20. TreeNode* node = st.top();
  21. if(node != NULL){
  22. st.pop();
  23. // 左
  24. if(node->left) st.push(node->left);
  25. // 中
  26. st.push(node);
  27. st.push(nullptr);
  28. // 右
  29. if(node->right) st.push(node->right);
  30. }else{
  31. st.pop();
  32. node = st.top();
  33. st.pop();
  34. node->val += pre;
  35. pre = node->val;
  36. }
  37. }
  38. return root;
  39. }
  40. };

note:我之前想着拿函数参数传值,,,然后就顺理成章地失败了。不如双指针。

以及这个中序遍历的倒叙版也是。。get到了

4.总结篇

代码随想录

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/1021226
推荐阅读
相关标签
  

闽ICP备14008679号