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

这篇博客介绍了如何在二叉搜索树中删除指定节点,提供了递归和迭代两种解法。递归解法通过比较目标值与当前节点值来决定在左子树还是右子树继续查找,找到节点后进行删除操作。迭代解法同样通过查找节点,但使用额外的变量来跟踪父节点,以便进行删除操作。在删除过程中,可能会遇到替换节点、没有孩子节点或有一个孩子节点的情况,解法都对此进行了处理。
摘要由CSDN通过智能技术生成

链接:https://leetcode.cn/problems/delete-node-in-a-bst/
二叉搜索树的增删改查都是必会的内容,但是删除相对于其他操作较难一点,可以通过这道题来进行练习。

题目描述

给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。

递归解法

class Solution:
    """
    递归解法
    """
    def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
        """
        删除根为root的BST中node.val等于key的节点,返回删除后的根节点(根节点可能变化)
        """
        if not root:
            return None
        if key < root.val:
            root.left = self.deleteNode(root.left, key)
        elif root.val < key:
            root.right = self.deleteNode(root.right, key)
        # # root可能有0或1个孩子节点,但是处理方式相同,返回孩子节点
        elif not root.left or not root.right:
            child = root.left if root.left else root.right
            return child
        else:
            successor = root.right  # 使用后继节点代替root
            while successor.left:
                successor = successor.left
            root.val = successor.val  # root.val赋值为后继节点值
            root.right = self.deleteNode(root.right, successor.val)
        return root

迭代解法

迭代解法基本思路与上相同,就是要多分类讨论删除的节点是不是根节点。

class Solution:
    """
    非递归解法
    """
    def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
        if not root:
            return root
        node, father = self.findNode(root, key)
        if not node:
            return root
        return self.delete(root, father, node)

    def delete(self, root, father, node):
        if node.left and node.right:
            suc_father, successor = self.findSuccessor(node)
            node.val = successor.val
            return self.delete(root, suc_father, successor)
        if not node.left and not node.right:
            if root == node:
                return None
            if father.left == node:
                father.left = None
            else:
                father.right = None
            return root
        child = node.left if node.left else node.right
        if root == node:
            return child
        if father.left == node:
            father.left = child
        else:
            father.right = child
        return root

    def findNode(self, root, key):
        if not root:
            return root
        if root.val == key:
            return root, None
        father = root
        while root:
            if root.val == key:
                return root, father
            elif key < root.val:
                father = root
                root = root.left
            else:
                father = root
                root = root.right
        return None, None

    def findSuccessor(self, root):
        successor = root.right
        father = root
        while successor.left:
            father = successor
            successor = successor.left
        return father, successor
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值