思路:对于空节点,应该返回true,对于任何一个节点,都应该小于某一个范围。对于左子树,该范围是left_num,root.val,对于右子树范围是root.val,right_num。不在范围内的一律返回FALSE。
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
def f(node,left_num,right_num):
if node is None:
return True
if left_num < node.val < right_num:
return f(node.left,left_num,node.val) and f(node.right,node.val,right_num)
return False
return f(root,-inf,inf)
class Solution:
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
#对二叉树进行中序遍历就可以得到一个递增序列。然后取递增序列的第k个值
ans = []
def dfs(node):
nonlocal ans
if node is None:
return
dfs(node.left)
ans.append(node.val)
dfs(node.right)
dfs(root)
return ans[k-1]
思路:用一个counter统计每个数字出现过的次数,最后将counter按照统计次数生成一个列表,取最高的出现次数,作为返回值的判断条件。
class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
import collections
ans = collections.Counter()
def bst(node):
nonlocal ans
if node is None:
return
bst(node.left)
ans[node.val] += 1
bst(node.right)
bst(root)
key = sorted(ans.items(), key=lambda s:(-s[1])) #按统计次数降序排序
return [i for i in ans if ans[i] == key[0][1]] #取最高的几个次数返回
思路:在中序遍历的时候,更新最小值和当前值。
class Solution:
min_mun = inf
pre = inf
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
def bst(node):
if node is None:
return
bst(node.left)
self.min_mun = min(abs(node.val-self.pre),self.min_mun)
self.pre = node.val
bst(node.right)
bst(root)
return self.min_mun
思路:边界条件是:如果root是空,就返回空,如果root.val==val,就返回该root。然后递归左子树,如果递归结果不是空,就返回这个结果,空则继续递归右子树,不空就返回结果。都为空,就返回None
class Solution:
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if root is None:
return None
if root.val == val:
return root
a = self.searchBST(root.left,val)
if a:return a
b = self.searchBST(root.right,val)
if b:return b
return None
后序遍历,当该树是空,返回-inf,inf,0,这样,在归的时候,这个条件就满足二叉搜索树,如果不满足二叉搜索树,就要返回inf,-inf,0,让l_max >= node.val or r_min <= node.val不成立。
class Solution:
def maxSumBST(self, root: Optional[TreeNode]) -> int:
max_mun = 0
def bst(node):
if node is None: #空也满足二叉搜索树
return -inf,inf,0
l_max,l_min,l_sum = bst(node.left)
r_max,r_min,r_sum = bst(node.right)
if l_max >= node.val or r_min <= node.val:#不是搜索树
return inf,-inf,0
nonlocal max_mun
max_mun = max(max_mun,r_sum+l_sum+node.val) #是搜索树就更新最大值
return max(r_max,node.val),min(l_min,node.val),r_sum+l_sum+node.val
bst(root)
return max_mun
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if root is None: #情况一,该节点是空,返回该节点
return root
if root is p or root is q: #情况二:如果该节点是p或者q,那返回该节点
return root
a = self.lowestCommonAncestor(root.left,p,q)
b = self.lowestCommonAncestor(root.right,p,q)
if a and b: #如果左右递归结果都存在,那pq分别在左右子树中,祖先就是当前值
return root
if not a and not b: #如果递归结果都不存在,那p,q不在该节点上,返回空
return None
if a and not b :return a
if b and not a :return b
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if root is q or root is p:
return root
if p.val < root.val and q.val < root.val:#在左子树中
return self.lowestCommonAncestor(root.left,p,q)
elif p.val > root.val and q.val > root.val:
return self.lowestCommonAncestor(root.right,p,q)
else:
return root