-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailycoding048.py
94 lines (69 loc) · 1.9 KB
/
dailycoding048.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
"""
This problem was asked by Google.
Given pre-order and in-order traversals of a binary tree,
write a function to reconstruct the tree.
solution
"""
class Node:
def __init__(self, val):
self.left = self.right = None
self.val = val
def construct_tree(inorder, preorder):
"""Recursively constructs a binary tree from and inorder & predorder traversal
"""
# base cases
if not inorder or not preorder:
return
root = Node(preorder[0])
if len(preorder) == 1:
return root
# construct the tree
for i, node in enumerate(inorder):
if node == root.val:
root.left = construct_tree(inorder[:i], preorder[1:i+1])
root.right = construct_tree(inorder[i+1:], preorder[i+1:])
return root
def inorder_it(root):
"""Returns list of inorder traversal for testing
"""
inorder, stack = [], []
curr = root
while stack or curr:
if not curr:
node = stack.pop()
inorder.append(node.val)
curr = node.right
else:
stack.append(curr)
curr = curr.left
return ''.join(inorder)
def preorder_it(root):
"""Returns list of preorder traversal for testing
"""
preorder, stack = [], []
stack.append(root)
while stack:
node = stack.pop()
preorder.append(node.val)
if node.right:
stack.append(node.right)
if node.left:
stack.append(node.left)
return ''.join(preorder)
def run_tests(tests):
result = True
for t in tests:
tree = construct_tree(*t)
result &= (t[0] == inorder_it(tree) and t[1] == preorder_it(tree))
return result
def main():
tests = (
("dbeafcg", "abdecfg"),
("dbeacg", "abdecg")
)
if run_tests(tests):
print("Passed")
else:
print("Failed")
if __name__ == '__main__':
main()