BST二叉搜索树之增删查验

二叉树算法的设计的总路线:明确一个节点要做的事情,然后剩下的事抛给框架。

98. 验证二叉搜索树

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        return self.isvalid(root, None, None)

    
    def isvalid(self, root, mini, maxi):
        if not root:
            return True
        if mini and root.val <= mini.val:
            return False
        if maxi and root.val >= maxi.val:
            return False
        return self.isvalid(root.left, mini, root) and self.isvalid(root.right, root, maxi)

在 BST 中查找一个数是否存在

不需要递归地搜索两边,类似二分查找思想,根据 target 和 root.val 的大小比较,就能排除一边。

def isInBST(root: TreeNode, target:int):
	if not root:
		return False
	if root.val == target:
		return True
	if root.val > target:
		return isInBST(root.left, target)
	if root.val < target:
		return isInBST(root.right, target)

在 BST 中插入一个数

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
        if not root:
            return TreeNode(val)
        if root.val < val:
            root.right = self.insertIntoBST(root.right, val)
        if root.val > val:
            root.left = self.insertIntoBST(root.left, val)
        return root

在 BST 中删除一个数

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
        if not root: # 空树
            return None
        if root.val == key: # 找到了要删除的节点
            if not root.left and not root.right: # 只有根节点,直接删除
                return None
            if not root.left: # 左子树为空,直接让右子树替代根节点
                return root.right
            if not root.right: # 右子树为空,直接让左子树替代根节点
                return root.left
            # 左右子树都不为空
            miniNode = self.getMin(root.right) # 找到右子树的最小值节点
            root.val = miniNode.val # 用右子树的最小值替换当前根节点的值 
            root.right = self.deleteNode(root.right, miniNode.val) # 同时删除右子树的最小值节点
        if root.val > key:
            root.left = self.deleteNode(root.left, key) # 递归寻找左子树进行删除
        if root.val < key:
            root.right = self.deleteNode(root.right, key) # 递归寻找右子树进行删除
        
        return root
    
    def getMin(self, root): # 寻找最小值节点
        while root.left:
            root = root.left
        return root

总结

二叉树算法设计的总路线:

  • 把当前节点要做的事做好,其他的交给递归框架,不用当前节点操心。
  • 如果当前节点会对下面的子节点有整体影响,可以通过辅助函数增长参数列表,借助参数传递信息。

针对 BST 的遍历框架:

def bst(root, target):
	if root.val == target:
		# do something
	if root.val < target:
		bst(root.right, target)
	if root.val > target:
		bst(root.left, target)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值