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

1.修建二叉搜索树

代码:递归法

class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int low, int high) {
        if(root == NULL) return NULL;
        // 中
        if(root->val > high){
            return trimBST(root->left,low,high);
        }
        if(root->val < low){
            return trimBST(root->right,low,high);
        }
        // 左
        root->left = trimBST(root->left,low,high);
        // 右
        root->right = trimBST(root->right,low,high);
        return root;
    }
};

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

代码:迭代法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int low, int high) {
        if(!root) return nullptr;
        while(root != nullptr && (root->val < low || root->val > high)){
            if(root->val < low) root = root->right;
            else root = root->left;
        }
        TreeNode* cur = root;
        while(cur != nullptr){
            while(cur->left && cur->left->val < low){
                cur->left = cur->left->right;
            }
            cur = cur->left;
        }
        cur = root;
        while(cur != nullptr){
            while(cur->right && cur->right->val > high){
                cur->right = cur->right->left;
            }
            cur = cur->right;
        }
        return root;
    }
};

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

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

代码:递归法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private: // 左闭右闭
    TreeNode* traversal(vector<int> &nums,int left,int right){
        // 递归出口
        if(left > right) return nullptr;
        int mid = left + (right - left) / 2;
        // 中
        TreeNode* node = new TreeNode(nums[mid]);
        // 左
        node->left = traversal(nums,left,mid - 1);
        // 右
        node->right = traversal(nums,mid + 1,right);
        return node;
    }
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        return traversal(nums,0,nums.size() - 1);
    }
};

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

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

代码:递归法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    int pre = 0;
    void traversal(TreeNode* node){
        if(node == NULL) return;
        // 右
        traversal(node->right);
        // 中
        node->val += pre;
        pre = node->val;
        // 左
        traversal(node->left);
    }
public:
    TreeNode* convertBST(TreeNode* root) {
        traversal(root);
        return root;
    }
};

 代码:迭代法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* convertBST(TreeNode* root) {
        stack<TreeNode*>st;
        if(root == NULL) return NULL;
        int pre = 0;
        st.push(root);
        while(!st.empty()){
            TreeNode* node = st.top();
            if(node != NULL){
                st.pop();
                // 左
                if(node->left) st.push(node->left);
                // 中
                st.push(node);
                st.push(nullptr);
                // 右
                if(node->right) st.push(node->right);
            }else{
                st.pop();
                node = st.top();
                st.pop();
                node->val += pre;
                pre = node->val;
            }
        }
        return root;
    }
};

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

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

4.总结篇

代码随想录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值