Skip to content

Commit b897fc8

Browse files
authored
Merge pull request #672 from sravanyabendi/binarytree-right
Binary Tree right side view
2 parents fc6d7d2 + 00618f6 commit b897fc8

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Python program to print right view of Binary Tree
2+
3+
# A binary tree node
4+
class Node:
5+
# A constructor to create a new Binary tree Node
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
#Function to print right view of Binary Tree
12+
def rightSideView(root):
13+
ans=[]
14+
# Base Case
15+
if not root:
16+
return ans
17+
else:
18+
l = []
19+
l.append(root)
20+
while l:
21+
for i in range(len(l)):
22+
node = l.pop(0)
23+
if node.left != None:
24+
l.append(node.left)
25+
if node.right != None:
26+
l.append(node.right)
27+
ans.append(node.val)
28+
return ans
29+
30+
root = Node(1)
31+
root.left = Node(2)
32+
root.right = Node(3)
33+
root.left.left = Node(None)
34+
root.left.right = Node(5)
35+
root.right.left = Node(None)
36+
root.right.right = Node(4)
37+
38+
print(rightSideView(root))
39+
40+
41+

0 commit comments

Comments
 (0)