代码随想录Day19


LeetCode235.二叉搜索树的最近祖先

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root->val >= p->val && root->val <= q->val || root->val >= q->val && root->val <= p->val)return root;
        if(root->val > p->val && root->val > q->val){
            return lowestCommonAncestor(root->left, p, q);
        }else {
            return lowestCommonAncestor(root->right, p, q);
        }
    }
};

LeetCode701.插入二叉搜索树

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        TreeNode* cur = root;
        if(!root){
            root = new TreeNode();
            root->val = val;
            return root;
        }
        while(cur){
            if(val > cur->val){
                if(cur->right != nullptr){
                    cur = cur->right;
                }else{
                    TreeNode* insertNode = new TreeNode();
                    insertNode->val = val;
                    cur->right = insertNode;
                    return root;
                }
            }else{
                if(cur->left != nullptr){
                    cur = cur->left;
                }else{
                    TreeNode* insertNode = new TreeNode();
                    insertNode->val = val;
                    cur->left = insertNode;
                    return root;
                }
            }
        }
        return nullptr;
    }
};

LeetCode450.删除二叉树的节点

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        TreeNode* dummy = new TreeNode();
        dummy->left = root;
        dummy->val = INT_MAX;
        TreeNode* pre = dummy;
        TreeNode* cur = root;
        bool isleft = true;
        while(cur){
            if(cur->val == key){
                if(!cur->right && cur->left){
                    if(isleft){
                        pre->left = cur->left;
                        cur->left = nullptr;
                        return dummy->left;
                    }else{
                        pre->right = cur->left;
                        cur->left = nullptr;
                        return dummy->left;
                    }
                }else if(cur->right && !cur->left){
                    if(isleft){
                        pre->left = cur->right;
                        cur->right = nullptr;
                        return dummy->left;
                    }else{
                        pre->right = cur->right;
                        cur->right = nullptr;
                        return dummy->left;
                    }
                }else if(!cur->right && !cur->left){
                    if(isleft){
                        pre->left = nullptr;
                        return dummy->left;
                    }else{
                        pre->right = nullptr;
                        return dummy->left;
                    }
                }else{
                    if(isleft){
                        pre->left = cur->left;
                    }else{
                        pre->right = cur->left;
                    }
                    TreeNode* cur2 = cur->left;
                    while(cur2->right){
                        cur2 = cur2->right;
                    }
                    cur2->right = cur->right;
                    cur->left = nullptr;
                    cur->right = nullptr;
                    return dummy->left;
                }
            }else{
                if(cur->val > key){
                    pre = cur;
                    cur = cur->left;
                    isleft = true;
                }else{
                    pre = cur;
                    cur = cur->right;
                    isleft = false;
                }
            }
        }
        return dummy->left;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值