目录
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