-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path067_rightSideView.py
More file actions
35 lines (32 loc) · 984 Bytes
/
067_rightSideView.py
File metadata and controls
35 lines (32 loc) · 984 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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
from queue import Queue
class Solution:
def rightSideView(self, root: TreeNode) -> List[int]:
if root == None:
return None
self.result = [root.val]
temp = []
q1 = Queue()
q2 = Queue()
q1.put(root)
while not q1.empty():
cur = q1.get()
for nodes in self._helper(cur):
if nodes == None:
continue
temp.append(nodes.val)
q2.put(nodes)
if q1.empty():
q1 = q2
q2 = Queue()
if temp != []:
self.result.append(temp[-1])
temp = []
return self.result
def _helper(self, node):
return [node.left, node.right]