-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy path0513-find-bottom-left-tree-value.py
44 lines (40 loc) · 1.19 KB
/
0513-find-bottom-left-tree-value.py
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
# 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
# Iterative
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
res = []
q = deque()
q.append(root)
while q:
qlen = len(q)
level = []
for i in range(qlen):
node = q.popleft()
if node:
q.append(node.left)
q.append(node.right)
level.append(node.val)
if level:
res.append(level)
return res[-1][0]
# recursive
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
max_height = -1
res = -1
def dfs(root, depth):
nonlocal max_height, res
if not root:
return
if depth > max_height:
max_height = max(depth, max_height)
res = root.val
dfs(root.left, depth + 1)
dfs(root.right, depth + 1)
dfs(root, 0)
return res