代码训练营第22天:二叉树part8|leetcode235 二叉搜索树的最近公共祖先|leetcode701 二叉搜索树中的插入操作|leetcode450 删除二叉搜索树中的节点。

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

文章讲解:leetcode235

leetcode701:二叉搜索树中的插入操作

文章讲解:leetcode701

leetcode450:删除二叉搜索树中的节点

文章讲解:leetcode450

目录

1,leetcode235 二叉搜索树的最近公共祖先:

2,leetcode701 二叉搜索树中的插入操作

3,leetcode450 删除二叉搜索树中的节点


1,leetcode235 二叉搜索树的最近公共祖先:

首先,这道题用二叉树的做法是肯定能做的:

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

bst是的特点是数值是有序的,意味着如果这个公共祖先存在,就一定介于p和q之间。

确实忽略了这样的性质。只要找到第一个介于p和q之间的就是找到的。

class Solution {
public:
    TreeNode* traverse(TreeNode* cur, TreeNode* p, TreeNode* q){
        if(cur == nullptr)return cur;
        else if(cur->val > max(p->val,q->val)){
            TreeNode* left = traverse(cur->left,p,q);
            if(left)return left;
        }
        else if(cur->val < min(p->val,q->val)){
            TreeNode* right = traverse(cur->right,p,q);
            if(right)return right;
        }
        return cur;
    }
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        return traverse(root,p,q);
    }
};

唯一需要注意的是这个移动逻辑。比两个都大就往小的找,往左走。比两个都小就往右边走。

2,leetcode701 二叉搜索树中的插入操作

常规操作了,但是写的时候还是遇到了一些bug,不过总体还是很简单的

class Solution {
public:
    void traverse(TreeNode* root, int val){
        if(root->left == nullptr || root->right == nullptr){
            if(root->val>val&&root->left == nullptr){
                TreeNode* node = new TreeNode(val);
                root->left = node;
                return;
            }
            if(root->val<val&&root->right == nullptr){
                TreeNode* node = new TreeNode(val);
                root->right = node;
                return;
            }  
        }
        if(root->val>val&&root->left){
            traverse(root->left, val);
        }
        if(root->val<val&&root->right){
            traverse(root->right, val);
        }

    }
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(root)traverse(root,val);
        else return new TreeNode(val);
        return root;
    }
};

代码随想录的代码更简洁:

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;
    }
};

免去了我在对于可插入节点的判断。走到nullptr的地方创建就好了。

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if (root == NULL) {
            TreeNode* node = new TreeNode(val);
            return node;
        }
        TreeNode* cur = root;
        TreeNode* parent = root; // 这个很重要,需要记录上一个节点,否则无法赋值新节点
        while (cur != NULL) {
            parent = cur;
            if (cur->val > val) cur = cur->left;
            else cur = cur->right;
        }
        TreeNode* node = new TreeNode(val);
        if (val < parent->val) parent->left = node;// 此时是用parent节点的进行赋值
        else parent->right = node;
        return root;
    }
};

至于这个迭代法应该是数据结构课上写的,有时间再复习一下。

3,leetcode450 删除二叉搜索树中的节点

数据结构课上最难的一个问题了,分三种情况,左右都空就直接删除,左右有一个空的就让那个非空的补上,左右都非空就让一个接在另一个上面。

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key)  {
        if (root == nullptr) return root;
        if (root->val == key) {
            if (root->left == nullptr && root->right == nullptr) {
                delete root;
                return nullptr;
            }
            else if (root->left == nullptr) {
                TreeNode* Node = root->right;
                delete root;
                return Node;
            }
            else if (root->right == nullptr) {
                TreeNode* Node = root->left;
                delete root;
                return Node;
            }
            else {
                TreeNode* cur = root->right;
                while(cur->left != nullptr) {
                    cur = cur->left;
                }
                cur->left = root->left; 
                TreeNode* tmp = root;  
                root = root->right;    
                delete tmp;             
                return root;
            }
        }
        if (root->val > key) root->left = deleteNode(root->left, key);
        if (root->val < key) root->right = deleteNode(root->right, key);
        return root;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值