forked from kumaya/python-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverseParentTree.py
45 lines (37 loc) · 1.13 KB
/
reverseParentTree.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
"""
@Problem Statement:
One of the many ways of representing a tree is to have an array(of length same as number of nodes),
where each element in the node denotes the parent of that node.
{-1, 0, 0, 1, 1} would represent a tree with -
* 0 as root
* 1 and 2 as children of 0
* 3 and 4 as children of 1
Given a similar representation, print reverse level order traversal of the corresponding tree.
Level order traversal of a tree is where we traverse levels of tree one by one.
@Input:
N: Length of tree
treeNode: representation of tree
"""
def reverse_parent_tree(tree, root):
# base case for recursive call
if len(tree) == 0:
return
ret = tree
tree = []
for node in ret:
if node in root:
tree += root[node]
reverse_parent_tree(tree, root)
print " ".join(ret)
# N = 5
# treeNode = ['-1', '0', '0', '2', '1']
N = 9
treeNode = ['8', '7', '0', '5', '5', '8', '7', '0', '-1']
parent = {}
for index,val in enumerate(treeNode):
if val in parent:
parent[val].append(str(index))
else:
parent[val] = [str(index)]
# print parent
reverse_parent_tree(parent['-1'], parent)