forked from netsetos/python_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbottom view
More file actions
29 lines (28 loc) · 1007 Bytes
/
bottom view
File metadata and controls
29 lines (28 loc) · 1007 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
def bottom_view(root):
if root is not None:
dict = {}
node_dict = {}
que = list()
que.append(root)
node_dict[root.data] = 0
while len(que) > 0:
temp = []
curr = que.pop(0)
if curr is not None:
level = node_dict[curr.data]
if level in dict:
temp = dict.get(level)
temp.clear()
temp.append(curr.data)
else:
temp.append(curr.data)
dict[level] = temp
if curr.left is not None:
left_node = curr.left
que.append(left_node)
node_dict[left_node.data] = node_dict[curr.data] - 1
if curr.right is not None:
right_node = curr.right
que.append(right_node)
node_dict[right_node.data] = node_dict[curr.data] + 1
print(dict)