forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathright_view.py
More file actions
42 lines (31 loc) · 965 Bytes
/
right_view.py
File metadata and controls
42 lines (31 loc) · 965 Bytes
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
"""
Print right view of a binary tree
The idea behind using max_level[0] is that -
1. Its value when changes will be reflected in every recursion call
2. We are visiting right side first. So this acts as a check that that level was done
"""
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def right_view_util(root, max_level, level):
if not root:
return
if max_level[0] < level:
print(root.val)
max_level[0] = level
right_view_util(root.right, max_level, level+1)
right_view_util(root.left, max_level, level+1)
def right_view(root):
max_level = [0]
right_view_util(root, max_level, 1)
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
root.right.left.right = Node(8)
right_view(root)