-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0513-find-bottom-left-tree-value.py
More file actions
37 lines (29 loc) · 891 Bytes
/
0513-find-bottom-left-tree-value.py
File metadata and controls
37 lines (29 loc) · 891 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
25
26
27
28
29
30
31
32
33
34
35
36
37
# time complexity: O(n)
# space complexity: O(n)
from collections import deque
from typing import Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
queue = deque()
current = root
queue.append(current)
while queue:
current = queue.popleft()
if current.right:
queue.append(current.right)
if current.left:
queue.append(current.left)
return current.val
root = TreeNode(1)
root.left = TreeNode(2)
root.left.left = TreeNode(4)
root.right = TreeNode(3)
root.right.left = TreeNode(5)
root.right.left.right = TreeNode(7)
root.right.right = TreeNode(6)
print(Solution().findBottomLeftValue(root))