代码随想录算法训练营第二十一天|235. 二叉搜索树的最近公共祖先、701. 二叉搜索树中的插入操作、450. 删除二叉搜索树中的节点

题目链接:235. 二叉搜索树的最近公共祖先

简单

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == nullptr || root == p || root == q) return root;

        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);
        if(left != nullptr && right != nullptr)  return root;
        else if(left == nullptr && right != nullptr) return right;
        else if (left!= nullptr && right == nullptr) return left;
        else return nullptr;
    }
};

题目链接:701. 二叉搜索树中的插入操作

简单

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(!root) return new TreeNode(val);
        insertBST(root, val);
        return root;
    }
    
    void insertBST(TreeNode* curr, int val) {
        if(val > curr->val) {
            if(curr->right) insertBST(curr->right, val);
            else curr->right = new TreeNode(val);
        }
        else{
            if(curr->left) insertBST(curr->left, val);
            else curr->left = new TreeNode(val);
        }
    }
};

题目链接:450. 删除二叉搜索树中的节点

分五种情况:搜索树中没有该节点、待删除节点为叶子节点、待删除节点左子树为空右子树不空、待删除节点右子树为空左子树不空、待删除节点左右子树均不为空。

自己写出一种迭代法:

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

迭代法中根节点与其他节点删除操作不相同,想到链表章节应对这种情况会加一个虚拟头结点,这里加入一个虚拟根节点。

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if(root == nullptr) return root;
        TreeNode* curr = root,* dummy_root = new TreeNode();
        dummy_root->left = root;
        TreeNode* pre = dummy_root;
        while(curr->val != key){
            pre = curr;
            if(curr->val > key) curr = curr->left;
            else curr = curr->right;
            if(curr == nullptr) return root;
        }
        if(curr->left == nullptr && curr->right == nullptr){
            if(pre->left == curr) pre->left = nullptr;
            else pre->right = nullptr;
        }
        else if(curr->left != nullptr && curr->right == nullptr) {
            if(pre->left == curr) pre->left = curr->left;
            else pre->right = curr->left;
        }
        else if(curr->left == nullptr) {
            if(pre->left == curr) pre->left = curr->right;
            else pre->right = curr->right;
        }
        else {
            TreeNode* temp = curr->right;
            while(temp->left != nullptr) temp = temp->left;
            temp->left = curr->left; 
            if(pre->left == curr) pre->left = curr->right;
            else pre->right = curr->right;
        }
        root = dummy_root->left;
        delete dummy_root;
        delete curr;
        return root;
    }
};

后来看了视频写出来递归的方法:

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if(root == nullptr) return nullptr;
        else if(root->val == key) {
            if(root->left == nullptr && root->right == nullptr) {
                delete root;
                return nullptr;
            }
            else if(root->left != nullptr && root->right == nullptr) {
                TreeNode* temp = root->left;
                delete root;
                return temp;
            }
            else if(root->left == nullptr) {
                TreeNode* temp = root->right;
                delete root;
                return temp;
            }
            else {
                TreeNode* temp = root->left;
                while(temp->right != nullptr) temp = temp->right;
                temp->right = root->right;
                temp = root->left;
                delete root;
                return temp;
            }
        }
        else if(root->val > key) {
            root->left = deleteNode(root->left, key);
            return root;
        }
        else {
            root->right = deleteNode(root->right, key);
            return root;
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值