二叉搜索树的插入&& 删除&&修剪

在这里插入图片描述

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(root == NULL){
            TreeNode* node = new TreeNode(val);
            return node;
        }

        if(root->val > val) root->left = insertIntoBST(root->left, val);
        if(root->val < val) root->right = insertIntoBST(root->right, val);

        return root;

    }
};

在这里插入图片描述

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        //case1:没找到,则返回null
        if(root == NULL) return root;
        //case2:找到了
        if(root->val == key){
            //1)左右子树都为空
            //2)左子树为空,右子树不为空
            if(!root->left) return root->right;
            //3)右子树为空,左子树不为空
            else if(!root->right) return root->left;
            else{ //4)左右子树都不为空
                //寻找右子树的最左边结点,并把root->left放到该结点的左结点上,最后返回root->right
                TreeNode* cur = root->right;
                while(cur->left!= NULL){
                    cur = cur->left;
                }
                cur->left = root->left;
                TreeNode* tmp = root;
                root = root->right;
                delete tmp;

                return root;
                
            }

        }

        if(key < root->val){
            root->left = deleteNode(root->left, key);
        }
        if(key > root->val){
            root->right = deleteNode(root->right, key);
        }

        return root;
    }
};

在这里插入图片描述

class Solution {
public:
//需要遍历整棵树
    TreeNode* trimBST(TreeNode* root, int low, int high) {
        if(root == nullptr) return NULL;

        if(root->val < low){
            TreeNode* left = trimBST(root->right, low, high);
            return left;
        }
        if(root->val > high){
            TreeNode* right = trimBST(root->left, low, high);
            return right;
        }

        //需要遍历整棵树,所以就不再上面前面加条件了,
        //和删除和插入的不同之处
        root->left = trimBST(root->left, low, high);
        root->right = trimBST(root->right, low, high);

        return root;

    }
};

在这里插入图片描述

class Solution {
public:
    TreeNode* convert(TreeNode* root, int &sum){
        if(!root) return root;

        root->right = convert(root->right, sum);

        root->val = sum + root->val;
        sum = root->val;

        root->left = convert(root->left, sum);

        return root;
    }
    TreeNode* convertBST(TreeNode* root) {
        if(!root) return root;
        int sum = 0;
        return convert(root, sum);
    }
};

在这里插入图片描述

class Solution {
public:
    TreeNode* createSearch(vector<int>& nums, int start, int end){
        //严格按照左闭又开的原则
        if(start == end) return NULL;
        int mid = (start + end) / 2;
        TreeNode* root = new TreeNode(nums[mid]);

        if(start + 1 == end) return root;

        root->left = createSearch(nums, start, mid);
        root->right = createSearch(nums, mid + 1, end);  

        return root;


    }
    TreeNode* sortedArrayToBST(vector<int>& nums) {

        return createSearch(nums, 0,  nums.size());
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值