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.
For example:
Given the following binary tree,
1 <--- / \ 2 3 <--- \ \ 5 4 <---
You should return [1, 3, 4]
.
class Solution:
# @param root, a tree node
# @return a list of integers
def rightSideView(self, root):
queue1 = []
queue2 = []
last_val_list = []
if(root is None):
return []
queue1.append(root)
while queue1:
root = queue1[0]
if root.left is not None:
queue2.append(root.left)
if root.right is not None:
queue2.append(root.right)
last_val = root.val
del queue1[0]
if queue1 == []:
queue1 = queue2
queue2= []
last_val_list.append(last_val)
return last_val_list