-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinary-tree-right-side-view.py
More file actions
72 lines (63 loc) · 3.01 KB
/
binary-tree-right-side-view.py
File metadata and controls
72 lines (63 loc) · 3.01 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# https://leetcode.com/problems/binary-tree-right-side-view/
# 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].
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# I think there might be a cleverer way to do this , but for now I have broken this down into two steps
# Step 1 : Get a list of list of the tree's Level Order Traversal
# Step 2 : Return the right most element of each sublist from the above superlist
class Solution(object):
def rightSideView(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if root is None:
return []
else:
levelorder = self.levelOrderTraversal(root)
final_result = []
l_o_l = len(levelorder)
for i in xrange(l_o_l):
final_result.append(levelorder[i][len(levelorder[i])-1])
return final_result
# This is exactly the same as the previously implemented level order traversal algorithm
def levelOrderTraversal(self,root):
if root is None:
return []
else:
processing_queue , return_list = [root] , []
while processing_queue:
level_vals = []
length = len(processing_queue)
for i in xrange(len(processing_queue)):
# *********************** Note ***********************
# This is a conceptual Queue , the head of the queue could be either on the left or the right hand side
# if the head of the queue is on the left - you dequeue by popping the element at index 0
# - you enqueue by appending to the right of the list
# if the head of the Queue is assumed to be on the right - you enqueue by inserting at index 0
# - you dequeue by popping the element on index(length of the list)
# For our purposes , we are assuming the head is one the left ( first option above)
# *********************** Note ***********************
temp_node = processing_queue[0]
level_vals.append(temp_node.val)
if temp_node.left:
processing_queue.append(temp_node.left)
if temp_node.right:
processing_queue.append(temp_node.right)
processing_queue.pop(0)
return_list.append(level_vals)
return return_list
my_solution = Solution()