class Solution:
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
self.ans = 999
self.pre = 999
def dfs(root):
if not root: return
dfs(root.left)
self.ans = min(self.ans, abs(self.pre - root.val))
self.pre = root.val
dfs(root.right)
dfs(root)
return self.ans
def findMode(self, root: Optional[TreeNode]) -> List[int]:
pp = collections.defaultdict(int)
def dfs(root):
if not root: return
pp[root.val] += 1
dfs(root.left)
dfs(root.right)
dfs(root)
mm = max(pp.values())
res = []
for i in pp.keys():
if pp[i] == mm:
res.append(i)
return res
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if root == p or root == q:
return root
if root:
left = self.lowestCommonAncestor(root.left,p,q)
right = self.lowestCommonAncestor(root.right,p,q)
if left and right:
return root
elif not left:
return right
else:
return left
return None
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47