-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinaryTreeRightSideView.py
48 lines (39 loc) · 1.07 KB
/
BinaryTreeRightSideView.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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.
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def rightSideView(root):
rightSide = []
q = []
if root == None:
return rightSide
q.append(root)
while q:
levelSize = len(q)
level = []
for x in range(levelSize):
curNode = q.pop(0)
level.append(curNode)
if curNode.left:
q.append(curNode.left)
if curNode.right:
q.append(curNode.right)
rightSide.append(level[len(level) - 1].val)
return rightSide
#Test cases
root = TreeNode(1)
node1 = TreeNode(2)
node2 = TreeNode(3)
node3 = TreeNode(5)
node4 = TreeNode(4)
root.left = node1
root.right = node2
node1.right = node3
node2.right = node4
print(rightSideView(root))
r = TreeNode(1)
node1 = TreeNode(2)
r.left = node1
print(rightSideView(r))