-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree_right_side_view.py
More file actions
33 lines (32 loc) · 961 Bytes
/
Copy pathbinary_tree_right_side_view.py
File metadata and controls
33 lines (32 loc) · 961 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
# 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 rightSideView(self, root: Optional[TreeNode]) -> List[int]:
if root is None:
return []
r = []
q = deque([root])
q.append(None)
while len(q) > 1:
temp = q.popleft()
if temp == None:
r.append(None)
q.append(None)
continue
else:
r.append(temp.val)
if temp.left != None:
q.append(temp.left)
if temp.right != None:
q.append(temp.right)
temp = []
for i in range(1,len(r)):
if r[i] == None:
temp.append(r[i-1])
if r[-1] != None:
temp.append(r[-1])
return temp