450. 删除二叉搜索树中的节点

方法一:迭代

1.1 findMin

class Solution
{
public:
    int findMin(TreeNode* root)
    {
        while (root->left != nullptr)
        {
            root = root->left;
        }

        return root->val;
    }

    TreeNode* deleteNode(TreeNode* root, int key)
    {
        TreeNode* dummy = new TreeNode{ -1,root,root };  //定义一个dummy节点是为了方便删除根节点
        TreeNode* parent = dummy;
        TreeNode* child = root;

        if (root == nullptr)   
        {
            return nullptr;
        }

        while (child != nullptr && key != child->val)
        {
            parent = child;

            if (key < child->val)
            {
                child = child->left;
            }
            else
            {
                child = child->right;
            }
        }

        if (child == nullptr)   //说明树中不存在val==key的节点,我们直接return
        {
            return root;
        }

        if (child->left != nullptr && child->right != nullptr)  //当该节点拥有两个child时
        {
            key = findMin(child->right);    //我们转化成删除右子树中的最小值,因此原来的key值应该变为findMin的返回值

            child->val = key;
            parent = child;
            child = child->right;
            while (key != child->val)
            {
                parent = child;

                if (key < child->val)
                {
                    child = child->left;
                }
                else
                {
                    child = child->right;
                }
            }
        }

        TreeNode* temp = child;

        if (child->left != nullptr)
        {
            child = child->left;
        }
        else if (child->right != nullptr)
        {
            child = child->right;
        }
        else
        {
            child = nullptr;
        }

        delete temp;

        if (key >= parent->val)    //注意等于是为了处理有且仅有一个右节点的情况
        {
            parent->right = child;
        }
        else
        {
            parent->left = child;
        }

        return dummy->right;
    }
};

1.2 findMax

class Solution
{
public:
    int findMax(TreeNode* root)
    {
        while (root->right != nullptr)
        {
            root = root->right;
        }

        return root->val;
    }

    TreeNode* deleteNode(TreeNode* root, int key)
    {
        TreeNode* dummy = new TreeNode{ 1000000,root,root };  
        TreeNode* parent = dummy;
        TreeNode* child = root;

        if (root == nullptr)
        {
            return nullptr;
        }

        while (child != nullptr && key != child->val)
        {
            parent = child;

            if (key < child->val)
            {
                child = child->left;
            }
            else
            {
                child = child->right;
            }
        }

        if (child == nullptr)
        {
            return root;
        }

        if (child->left != nullptr && child->right != nullptr)
        {
            key = findMax(child->left);

            child->val = key;
            parent = child;
            child = child->left;
            while (key != child->val)
            {
                parent = child;

                if (key < child->val)
                {
                    child = child->left;
                }
                else
                {
                    child = child->right;
                }
            }
        }

        TreeNode* temp = child;

        if (child->left != nullptr)
        {
            child = child->left;
        }
        else if (child->right != nullptr)
        {
            child = child->right;
        }
        else
        {
            child = nullptr;
        }

        delete temp;

        if (key <= parent->val)
        {
            parent->left = child;
        }
        else
        {
            parent->right = child;
        }

        return dummy->left;
    }
};

方法二:递归

2.1 findMin

class Solution
{
public:
    int findMin(TreeNode*root)
    {
        if(root->left!=nullptr)
        {
            return findMin(root->left);
        }

        return root->val;
    }

    TreeNode* deleteNode(TreeNode* root, int key)
    {
        if (root == nullptr)
        {
            return nullptr;
        }
        else if (key < root->val)
        {
            root->left = deleteNode(root->left, key);
        }
        else if (key > root->val)
        {
            root->right = deleteNode(root->right, key);
        }
        else
        {
            if (root->left != nullptr && root->right != nullptr)
            {
                root->val = findMin(root->right);
                root->right=deleteNode(root->right, root->val);
            }
            else
            {
                TreeNode* temp = root;
                if (root->left != nullptr)
                {
                    root = root->left;
                }
                else if (root->right != nullptr)
                {
                    root = root->right;
                }
                else
                {
                    root = nullptr;
                }
                delete temp;
            }
        }

        return root;
    }
};

2.2 findMax

class Solution
{
public:
    int findMax(TreeNode*root)
    {
        if(root->right!=nullptr)
        {
            return findMax(root->right);
        }

        return root->val;
    }

    TreeNode* deleteNode(TreeNode* root, int key)
    {
        if (root == nullptr)
        {
            return nullptr;
        }
        else if (key < root->val)
        {
            root->left = deleteNode(root->left, key);
        }
        else if (key > root->val)
        {
            root->right = deleteNode(root->right, key);
        }
        else
        {
            if (root->left != nullptr && root->right != nullptr)
            {
                root->val = findMax(root->left);
                root->left=deleteNode(root->left, root->val);
            }
            else
            {
                TreeNode* temp = root;
                if (root->left != nullptr)
                {
                    root = root->left;
                }
                else if (root->right != nullptr)
                {
                    root = root->right;
                }
                else
                {
                    root = nullptr;
                }
                delete temp;
            }
        }

        return root;
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值