-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Right_view_of_binary_tree.py
105 lines (94 loc) · 2.22 KB
/
Right_view_of_binary_tree.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'''
PROBLEM STATEMENT:
Given a binary tree, the task is to print its right view. Right view of
a binary tree is defined as the nodes which will be visible if the tree
is viewed from the right side. The input for the binary tree is in the
form of preorder and entering '-1' denotes a null node.
For example:
Input: 3 4 -1 6 -1 -1 5 1 -1 -1 -1
The above input will have the following structure:
3
/ \
4 5
\ /
6 1
Output: 3 5 1, as as these are the nodes that will be visible from right.
'''
# A class to create a node
class Node:
# Constructor to initialize node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# A function to build the tree in preorder form
def BuildTree():
d = int(input())
if d == -1:
return None
root = Node(d)
root.left = BuildTree()
root.right = BuildTree()
return root
# A function to print the right view of the binary tree
def RightView(root, level, maxlevel):
# base case
if root is None:
return
if level > maxlevel[0]:
print(root.data, end=" ")
maxlevel[0] = level
# Recursive case
RightView(root.right, level + 1, maxlevel)
RightView(root.left, level + 1, maxlevel)
print("Enter values in a binary tree:")
# A function call to build the tree and return root node
root = BuildTree()
# maxlevel is defined as a list because it is mutable and we want the changes
# made to it gets reflected outside the function
maxlevel = [-1]
print("Right view of the binary tree is:")
RightView(root, 0, maxlevel)
'''
TEST CASE:
1.
Input:
Enter values in a binary tree:
2
4
7
8
-1
-1
-1
5
-1
-1
3
9
-1
6
-1
1
-1
-1
-1
Output:
Right view of the binary tree is:
2 3 9 6 1
Explanation:
The structure of the tree is:
2
/ \
4 3
/ \ /
7 5 9
/ \
8 6
\
1
Therefore, the nodes visible from the right are: 2 3 9 6 1
TIME COMPLEXITY: O(n), due to the traversal of the whole tree,
where 'n' denotes the number of nodes in the tree.
SPACE COMPLEXITY: O(1), no extra space used.
'''