-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathBottomViewOfATree.py
73 lines (65 loc) · 2.15 KB
/
BottomViewOfATree.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
from sortedcontainers import SortedDict
"""
@author Anirudh Sharma
Given a binary tree, print the bottom view from left to right.
A node is included in bottom view if it can be seen when we look at the tree from bottom.
Constraints:
1 <= Number of nodes <= 10^5
1 <= Data of a node <= 10^5
"""
def bottomView(root):
# List to store the result
result = []
# Special case
if root is None:
return result
# Horizontal distance for a node.
# It will be zero for the root node.
horizontalDistance = 0
# TreeMap to store node's data, height and
# horizontal distance
treeMap = SortedDict()
# Queue to store the nodes in level order traversal
nodes = []
# Initialize the values
root.horizontalDistance = horizontalDistance
nodes.append(root)
# Loop until the queue is empty
while nodes:
# Get the current node from the head
current = nodes.pop(0)
# Get the current horizontal distance
horizontalDistance = current.horizontalDistance
# Put the dequeued tree node to TreeMap having key
# as horizontal distance. Every time we find a node
# having same horizontal distance we need to replace
# the data in the map.
treeMap[horizontalDistance] = current.data
# Check for the left and right children
if current.left:
current.left.horizontalDistance = horizontalDistance - 1
nodes.append(current.left)
if current.right:
current.right.horizontalDistance = horizontalDistance + 1
nodes.append(current.right)
# Populate the result list
for value in treeMap.values():
result.append(value)
return result
class Node:
def __init__(self, data):
self.data = data
self.horizontalDistance = 0
self.left = None
self.right = None
if __name__ == "__main__":
root = Node(20)
root.left = Node(8)
root.right = Node(22)
root.left.left = Node(5)
root.left.right = Node(3)
root.right.left = Node(4)
root.right.right = Node(25)
root.left.right.left = Node(10)
root.left.right.right = Node(14)
print(bottomView(root))