蓝桥杯 第8天 二叉搜索树

目录

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

2.98. 验证二叉搜索树 - 力扣(LeetCode) (leetcode-cn.com)

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

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

5.236. 二叉树的最近公共祖先 - 力扣(LeetCode) (leetcode-cn.com)


1.700. 二叉搜索树中的搜索 - 力扣(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 searchBST(self, root: TreeNode, val: int) -> TreeNode:
        if root==None or root.val==val:
            return root
        if root.val>val:
            return self.searchBST(root.left,val)
        else:
            return self.searchBST(root.right,val)
# 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 searchBST(self, root: TreeNode, val: int) -> TreeNode:
        while root:
            if root.val<val:
                root=root.right
            elif root.val>val:
                root=root.left
            else:
                return root
        return root

2.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:
        def dfs(root,lower=-float("inf"),upper=float("inf")):
            if not root:
                return True

            val=root.val
            if val<=lower or val>=upper:
                return False
            l=dfs(root.left,lower,root.val)
            r=dfs(root.right,root.val,upper)
            return l and r
        return dfs(root)
# 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:
        s=[]
        cur=root
        pre=None
        while cur or s:
            if cur:
                s.append(cur)
                cur=cur.left
            else:
                cur=s.pop()
                if pre and pre.val>=cur.val:
                    return False
                pre=cur
                cur=cur.right
        return True

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

class Solution:
    def getMinimumDifference(self, root: TreeNode) -> int:
        martix=[]
        def build(root):
            if root.left:
                build(root.left)
            martix.append(root.val)
            if root.right:
                build(root.right)
            return martix
        build(root)
        ans=1<<31
        for i in range(len(martix)-1):
            ans=min(ans,abs(martix[i]-martix[i+1]))
        return ans
# 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:
        s=[]
        cur=root
        ans=1<<31
        pre=None
        while cur or s:
            if cur:
                s.append(cur)
                cur=cur.left
            else:
                cur=s.pop()
                if pre and cur.val-pre.val<ans:
                    ans=cur.val-pre.val
                pre=cur
                cur=cur.right
        return ans

4.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]:
        stack=[]
        cur=root
        pre=None
        result=[]
        maxcount=-1
        count=0
        while cur or stack:
            if cur:
                stack.append(cur)
                cur=cur.left
            else:
                cur=stack.pop()
                if pre==None:
                    count=1
                elif pre.val==cur.val:
                    count+=1
                else:
                    count=1
                if count==maxcount:
                    result.append(cur.val)
                elif count>maxcount:
                    result.clear()
                    result.append(cur.val)
                    maxcount=count
                pre=cur
                cur=cur.right
        return result

5.236. 二叉树的最近公共祖先 - 力扣(LeetCode) (leetcode-cn.com)

# 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':
        if not root or root==p or root==q:
            return root
        left=self.lowestCommonAncestor(root.left,p,q)
        right=self.lowestCommonAncestor(root.right,p,q)

        if left and right:
            return root
        if not left and right:
            return right
        return left

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值