-
Notifications
You must be signed in to change notification settings - Fork 3
/
48_Google_Reconstruct_Tree.py
executable file
·145 lines (99 loc) · 3.12 KB
/
48_Google_Reconstruct_Tree.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
This problem was asked by Google.
Given pre-order and in-order traversals of a binary tree, write a function to reconstruct the tree.
For example, given the following preorder traversal:
[a, b, d, e, c, f, g]
And the following inorder traversal:
[d, b, e, a, f, c, g]
You should return the following tree:
a
/ \
b c
/ \ / \
d e f g
"""
class node:
def __init__(self, data=None, right=None, left=None):
self.data = data
self.right = right
self.left = left
def inorder(root):
list_of_nodes = []
def recursive_helper(node):
if node.left:
recursive_helper(node.left)
list_of_nodes.append(node.data)
if node.right:
recursive_helper(node.right)
recursive_helper(root)
return list_of_nodes
def preorder(root):
list_of_nodes = []
def recursive_helper(node):
list_of_nodes.append(node.data)
if node.left:
recursive_helper(node.left)
if node.right:
recursive_helper(node.right)
recursive_helper(root)
return list_of_nodes
def postorder(root):
list_of_nodes = []
def recursive_helper(node):
if node.left:
recursive_helper(node.left)
if node.right:
recursive_helper(node.right)
list_of_nodes.append(node.data)
recursive_helper(root)
return list_of_nodes
def make_tree(preorder, inorder):
if not preorder or not inorder:
return None
root = node(preorder[0])
if len(preorder) == 1:
return root
for i, char in enumerate(inorder):
if preorder[0] == char:
root.left = make_tree(preorder=preorder[1:i+1], inorder=inorder[:i])
root.right= make_tree(preorder=preorder[i+1:], inorder=inorder[i+1:])
return root
"""
tree_1:
a
/ \
b c
/ \ / \
d e f g
tree_2:
a
/ \
b c
/ \ / \
d e f g
/ \ / \
h i j k
\
l
"""
if __name__ == '__main__':
# create tree and then check against its post, pre and in order lists
tree_1 = node('a',
left=node('b', left=node('d'), right=node('e')),
right=node('c', left=node('f'), right=node('g')))
created_tree = make_tree(preorder=preorder(tree_1), inorder=inorder(tree_1))
assert preorder(created_tree) == preorder(tree_1)
assert inorder(created_tree) == inorder(tree_1)
assert postorder(created_tree) == postorder(tree_1)
tree_2 = node('a',
left=node('b',
left=node('d', left=node('h'), right=node('i')),
right=node('e')),
right=node('c',
left=node('f'),
right=node('g', left=node('j'), right=node(data='k', right=node('l'))))
)
created_tree = make_tree(preorder=preorder(tree_2), inorder=inorder(tree_2))
assert preorder(created_tree) == preorder(tree_2)
assert inorder(created_tree) == inorder(tree_2)
assert postorder(created_tree) == postorder(tree_2)