-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path199_binary_tree_right_side_view_medium.py
More file actions
70 lines (62 loc) · 1.97 KB
/
199_binary_tree_right_side_view_medium.py
File metadata and controls
70 lines (62 loc) · 1.97 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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:
"""
Description:
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
Example:
Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:
1 <---
/ \
2 3 <---
\ \
5 4 <---
Idea:
A: Use queue to get all items from level
B: Use queue and also pass current level. We start from right, and compare the current level with the length of result
Complexity:
A:
Time: O(N)
Space: O(N)
B:
Time: O(N)
Space: O(N)
"""
def rightSideView(self, root: TreeNode) -> List[int]:
# if not root:
# return []
# queue = [[root]]
# res = []
# while queue:
# level = queue.pop(0)
# new_level = []
# for node in level:
# if node.left:
# new_level.append(node.left)
# if node.right:
# new_level.append(node.right)
# if new_level:
# queue.append(new_level)
# res.append(new_level)
# final_res = [root.val]
# for entry in res:
# final_res.append(entry[-1].val)
# return final_res
if not root:
return []
res = []
queue = [(root, 0)]
while queue:
node, level = queue.pop(0)
if node:
if len(res) == level:
res.append(node.val)
queue.append((node.right, level + 1))
queue.append((node.left, level + 1))
return res