forked from netsetos/python_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTop View
More file actions
25 lines (24 loc) · 867 Bytes
/
Top View
File metadata and controls
25 lines (24 loc) · 867 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
def top_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 not in dict:
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)