力扣刷题-python-二叉树-4(二叉搜索树、查找、插值、删除、修改)

文章目录

    • 1.验证二叉搜索树
    • 2.二叉搜索树的最小绝对差
    • 3.二叉搜索树的插入和删除
    • 4.将有序数组恢复成二叉搜索树
    • 5.二叉树值的修改
    • 6.总结

1.验证二叉搜索树

98. 验证二叉搜索树 - 力扣(LeetCode) (leetcode-cn.com)
用了递归法和迭代法

# 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 isValidBST(self, root: TreeNode) -> bool:
        #规律: BST的中序遍历节点数值是从小到大
        maxval = -float("inf")
        def _isValidBST(root: TreeNode)->bool:
            nonlocal maxval
            if not root:return True
            #左中右 中序
            leftbool= _isValidBST(root.left)
            if root.val > maxval :maxval=root.val
            else:return False
            rightbool= _isValidBST(root.right)

            return leftbool and rightbool
        return _isValidBST(root)
'''
class Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        #中序将所有数 读出来
        stacker = [root]
        leftres= -float("inf")
        while stacker:
            noder= stacker.pop()
            if noder:  #因为是栈 所以用左中右的倒序
                 if noder.right:stacker.append(noder.right)
                 stacker.append(noder)
                 stacker.append(None)
                 if noder.left:stacker.append(noder.left)
            else:
                noder= stacker.pop()
                if leftres>=noder.val:return False
                else:leftres = noder.val
        return True

2.二叉搜索树的最小绝对差

530. 二叉搜索树的最小绝对差 - 力扣(LeetCode) (leetcode-cn.com)

# 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 getMinimumDifference(self, root: TreeNode) -> int:
        leftval=-float(inf) #定义一个左边值  递归法
        def _getMinimumDifference(root: TreeNode):
            #中序搜索
            nonlocal leftval
            if not root:return float(inf)
            leftm= _getMinimumDifference(root.left)
            res = root.val-leftval
            leftval = root.val
            righm= _getMinimumDifference(root.right)
            return min(leftm,res,righm)
        return _getMinimumDifference(root)
'''
class Solution:
    def getMinimumDifference(self, root: TreeNode) -> int:
         stacker = [root]
         leftval =-float(inf)
         minval= float(inf)
         while stacker:
             noder= stacker.pop()
             if noder:
                 if noder.right: stacker.append(noder.right)
                 stacker.append(noder)
                 stacker.append(None)
                 if noder.left:  stacker.append(noder.left)
             else:
                 noder = stacker.pop()
                 minval = min(noder.val-leftval, minval)
                 leftval = noder.val
         return minval

501. 二叉搜索树中的众数 - 力扣(LeetCode) (leetcode-cn.com)

# 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 findMode(self, root: TreeNode) -> List[int]:
        leftval = None
        maxcount= 1
        stacker = [root]
        res=[]
        counter=1
        while stacker:
            noder= stacker.pop()
            if noder:
                if noder.right:stacker.append(noder.right)
                stacker.append(noder)
                stacker.append(None)
                if noder.left: stacker.append(noder.left)
            else:
                noder = stacker.pop()
                if leftval!= noder.val:
                    if counter>maxcount:
                        res=[leftval]
                        maxcount= counter
                    elif counter ==maxcount and leftval: res.append(leftval)
                    leftval =noder.val
                    counter=1
                else:
                    counter+=1
        if counter> maxcount: res=[noder.val]         #补上结尾的
        elif counter== maxcount:res.append(noder.val)
        return  res

235. 二叉搜索树的最近公共祖先 - 力扣(LeetCode) (leetcode-cn.com)
比236简单多了,因为二叉搜索树自带方向,遇见二叉搜索树可以尝试用这个性质。

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

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        '''
        #从上到下遍历 数值包含在pq 之间 说明为最近公共祖先
        #也就是层序遍历 再加判断
        if p.val>q.val:p,q=q,p #如果不是前大后小 交换
        queue = deque([root])
        while queue:
            noder = queue.popleft()
            if p.val<=noder.val<=q.val: return noder
            if noder.left:queue.append(noder.left)
            if noder.right:queue.append(noder.right)
        '''
        '''
        #用到二叉搜索树的特点 不在之间就是偏向于一边  再根据偏向的方向 迭代
        while 1:
            if   root.val>p.val and root.val>q.val: root= root.left
            elif root.val<q.val and root.val <p.val: root =root.right
            else:return root
        '''
        #同理可以用递归法
        if root.val>p.val and root.val>q.val: return self.lowestCommonAncestor(root.left,p,q)
        if root.val<q.val and root.val <p.val:return self.lowestCommonAncestor(root.right,p,q)
        return root

3.二叉搜索树的插入和删除

701. 二叉搜索树中的插入操作 - 力扣(LeetCode) (leetcode-cn.com)

# 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:
        insetval = TreeNode(val)
        if not root:return insetval
        rootls = root
        while 1:
            if val>rootls.val: 
                if rootls.right:
                    rootls= rootls.right
                else:
                    rootls.right=insetval
                    break
            else: 
                if rootls.left: 
                    rootls= rootls.left
                else:
                    rootls.left=insetval
                    break
        return root
'''
class Solution:
    def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
        if not root: return TreeNode(val)  #递归的终止
        if root.val>val: root.left= self.insertIntoBST(root.left,val)
        if root.val<val: root.right=self.insertIntoBST(root.right,val)
        return root

450. 删除二叉搜索树中的节点 - 力扣(LeetCode) (leetcode-cn.com)
删除要比添加难,因为删除时候还要考虑双孩子都在情况 具体是给那个
如下面删除7情况就是特殊情况之一,也是最难的一种情况,需要将左孩子节点放到右孩子节点最底层的左孩孩孩子节点下面
力扣刷题-python-二叉树-4(二叉搜索树、查找、插值、删除、修改)_第1张图片

# 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 deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
        if not root:return root    #第一种情况  没有找到 返回空节点
        if not root.val-key:  
            if not root.left and not root.right:return None  #第二种情况 两孩子节点都为空 直接返回none
            if not root.left and     root.right:return root.right  #第三种情况 右孩子不为空 返回有孩子节点
            if     root.left and not root.right:return root.left   #第四种情况 左孩子不为空 同上情况
            else:#第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置
                noder = root.right
                while noder.left: noder = noder.left
                noder.left = root.left
                return root.right
        if root.val>key:root.left = self.deleteNode(root.left,key)  #如果key 比root的值小,说明这个值在右孩子节点
        if root.val<key:root.right= self.deleteNode(root.right,key)
        return root

4.将有序数组恢复成二叉搜索树

108. 将有序数组转换为二叉搜索树 - 力扣(LeetCode) (leetcode-cn.com)

# 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 sortedArrayToBST(self, nums: List[int]) -> TreeNode:
        if not nums:return None
        centIndex= len(nums)//2
        root = TreeNode(nums[centIndex])

        root.left = self.sortedArrayToBST(nums[:centIndex])     #前半拉是左孩子
        root.right= self.sortedArrayToBST(nums[centIndex+1:])   
        return root

5.二叉树值的修改

538. 把二叉搜索树转换为累加树 - 力扣(LeetCode) (leetcode-cn.com)
可以用倒中序 右中左 遍历的同时将值加上去

# 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 convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        #试下中序的倒序  右中左
        if not root: return root
        stacker = [root]
        sumVal=0
        while stacker:
            noder= stacker.pop()
            if noder:
                if noder.left: stacker.append(noder.left)
                stacker.append(noder)
                stacker.append(None)
                if noder.right: stacker.append(noder.right)
            else:
                noder = stacker.pop()
                sumVal+=noder.val
                noder.val= sumVal
        return root

6.总结

二叉树终于完成了,总共花费了4天时间,基本都可以用递归和迭代两种方法解决,但是针对于题,要偏向于某一种相对较容易解决。这里做个标记防止以后自己回来看,层序的要用队列,前中后序的要用栈。

你可能感兴趣的:(python(力扣)-刷题笔记,leetcode,python,算法)