算法训练营第22天|● 235. 二叉搜索树的最近公共祖先 ● 701.二叉搜索树中的插入操作 ● 450.删除二叉搜索树中的节点

文档讲解:代码随想录 (programmercarl.com)

视频讲解:代码随想录的个人空间-代码随想录个人主页-哔哩哔哩视频 (bilibili.com)

LeetCode 二叉搜索树的最近公共祖先

题目链接:235. 二叉搜索树的最近公共祖先 - 力扣(LeetCode)

代码如下:

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

LeetCode 二叉搜索树中的插入操作

题目链接:701. 二叉搜索树中的插入操作 - 力扣(LeetCode)

解题思路:迭代法,按部就班。

解题代码如下: 

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        TreeNode* cur=root;
        TreeNode *tmp=new TreeNode(val);
        while(cur){
            if(cur->val<val){
                if(!cur->right){
                    cur->right=tmp;
                    return root;
                }
                cur=cur->right;
            }
            else {
                if(!cur->left){
                    cur->left=tmp;
                    return root;
                }
                cur=cur->left;
            }
        }
        if(!root)return tmp;
        return root;
    }
};
class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(!root){
            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;
    }
};

LeetCode 删除二叉搜索树中的节点

题目链接:450. 删除二叉搜索树中的节点 - 力扣(LeetCode)

解题思路:迭代法:画图,按部就班,先找出节点,再进行删除操作。

解题代码如下:

class Solution {
public:
    TreeNode* deleteoneNode(TreeNode* cur){
        if(!cur)return cur;
        if(!cur->right)return cur->left;
        TreeNode* curright=cur->right;
        while(curright->left)curright=curright->left;
        curright->left=cur->left;
        return cur->right;
    }
    TreeNode* deleteNode(TreeNode* root, int key) {
        TreeNode* cur=root;
        TreeNode* pre=nullptr;
        while(cur){
            if(cur->val==key)break;
            pre=cur;
            if(cur->val>key)cur=cur->left;
            else cur=cur->right;
        }
        if(cur==root)return deleteoneNode(cur);
        if(pre->left&&pre->left->val==key)pre->left=deleteoneNode(cur);
        if(pre->right&&pre->right->val==key)pre->right=deleteoneNode(cur);
        return root;
    }
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值