-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path199.py
More file actions
30 lines (23 loc) · 891 Bytes
/
Copy path199.py
File metadata and controls
30 lines (23 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
"""Given the root of 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."""
# 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]:
output = []
q = collections.deque([root])
while q:
rightnode = None
lenq = len(q)
for i in range(lenq):
node = q.popleft()
if node:
rightnode = node
q.append(node.left)
q.append(node.right)
if rightnode:
output.append(rightnode.val)
return output