-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path144.py
More file actions
24 lines (19 loc) · 677 Bytes
/
144.py
File metadata and controls
24 lines (19 loc) · 677 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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 lcaDeepestLeaves(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
def dfs(root: TreeNode):
if not root:
return 0, None
left = dfs(root.left)
right = dfs(root.right)
if left[0] > right[0]:
return left[0] + 1, left[1]
if left[0] < right[0]:
return right[0] + 1, right[1]
return left[0] + 1, root
return dfs(root)[1]