Skip to content

Commit 8383704

Browse files
authored
RightViewofBinaryTree
Print the values of nodes which are present at right side of binary tree. This traversal can be done by using other traversals like pre-order, post-order and In-order.
1 parent 7901752 commit 8383704

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

RightViewofBinaryTree

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Node:
2+
def __init__(self,key):
3+
self.left=None
4+
self.right=None
5+
self.value=key
6+
class BinaryTree:
7+
def __init__(self):
8+
self.root1=None
9+
#used to the tree from class Node and its object root
10+
def _inorder_recursive(self,node):
11+
if node:
12+
self._inorder_recursive(node.left)
13+
print(node.value,end="->")
14+
self._inorder_recursive(node.right)
15+
#RIGHT SIDE OF BINARY TREE
16+
def rightSideView(self,root,level=0,ans=None):
17+
if ans is None:
18+
ans=[]
19+
if root is None:
20+
return ans
21+
if len(ans)==level:
22+
ans.append(root.value)
23+
self.rightSideView(root.right,level+1,ans)
24+
self.rightSideView(root.left,level+1,ans)
25+
return ans
26+
#Create nodes
27+
root=Node(10)
28+
root.left=Node(34)
29+
root.right=Node(20)
30+
root.left.left=Node(3)
31+
root.left.right=Node(4)
32+
root.right.left=Node(12)
33+
root.right.right=Node(54)
34+
tree=BinaryTree()
35+
tree.root1=root
36+
print(tree._inorder_recursive(tree.root1))
37+
print("Right side view:",tree.rightSideView(tree.root1))

0 commit comments

Comments
 (0)