-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathTopViewOfATree.py
82 lines (67 loc) · 2.22 KB
/
TopViewOfATree.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
from sortedcontainers import SortedDict
"""
@author Anirudh Sharma
Given a pointer to the root of a binary tree, print the top view of the binary tree.
The tree as seen from the top the nodes, is called the top view of the tree.
Constraints:
1 <= N <= 10^5
1 <= Node Data <= 10^5
"""
def topViewHelper(root, verticalDistance, horizontalDistance, treeMap):
# Base condition
if root is None:
return
# If the node for a horizontal distance is not
# present in the map, add it.
if treeMap.get(horizontalDistance) is None:
treeMap[horizontalDistance] = Pair(root.data, verticalDistance)
# If the node for a horizontal distance is present,
# then we will pick up the node that comes first
else:
currentPair = treeMap[horizontalDistance]
if currentPair.height >= verticalDistance:
treeMap[horizontalDistance] = Pair(root.data, verticalDistance)
# Recursive calls to left and right subtrees
topViewHelper(root.left, verticalDistance + 1,
horizontalDistance - 1, treeMap)
topViewHelper(root.right, verticalDistance + 1,
horizontalDistance + 1, treeMap)
def topView(root):
# List to store the result
result = []
# TreeMap to store node's data, height and
# horizontal distance
treeMap = SortedDict()
topViewHelper(root, 0, 0, treeMap)
for value in treeMap.values():
result.append(value.nodeData)
return result
class Pair:
def __init__(self, nodeData, height):
self.nodeData = nodeData
self.height = height
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
if __name__ == "__main__":
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.left.left = Node(8)
root.left.left.right = Node(9)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
print(topView(root))
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.right = Node(4)
root.right.left = Node(5)
root.right.right = Node(6)
root.right.left.left = Node(7)
root.right.left.right = Node(8)
print(topView(root))