-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path199.py
32 lines (29 loc) · 954 Bytes
/
199.py
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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param {TreeNode} root
# @return {integer[]}
def rightSideView(self, root):
if root == None:
return []
stack = [root]
traversal = []
while len(stack) > 0 :
level_stack = []
level_traversal = []
for node in stack:
level_traversal.append(node.val)
if node.left != None:
level_stack.append(node.left)
if node.right !=None:
level_stack.append(node.right)
stack = level_stack[:]
traversal.append(level_traversal)
rightview = []
for level_traversal in traversal:
rightview.append(level_traversal[-1])
return rightview