-
Notifications
You must be signed in to change notification settings - Fork 0
/
048.py
68 lines (53 loc) · 1.65 KB
/
048.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
"""
Problem:
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
"""
from typing import List
from DataStructures.Tree import BinaryTree, Node
def generate_tree(preorder: List[int], inorder: List[int]) -> BinaryTree:
length = len(preorder)
if length != len(inorder):
raise RuntimeError
if length == 0:
return BinaryTree()
# generating the root
root = preorder[0]
tree = BinaryTree()
tree.root = Node(root)
# generating the rest of the tree
if length > 1:
i = inorder.index(root)
# partitioning the nodes as per the branch
inorder_left, preorder_left = (inorder[:i], preorder[1 : i + 1])
inorder_right, preorder_right = (inorder[i + 1 :], preorder[i + 1 :])
# creating a tree for each branch
tree_left = generate_tree(preorder_left, inorder_left)
tree_right = generate_tree(preorder_right, inorder_right)
# attaching the sub-tree to their respective branch
tree.root.left = tree_left.root
tree.root.right = tree_right.root
return tree
if __name__ == "__main__":
test1 = generate_tree(
["a", "b", "d", "e", "c", "f", "g"], ["d", "b", "e", "a", "f", "c", "g"]
)
print(test1)
test2 = generate_tree(
["a", "b", "d", "e", "c", "f"], ["d", "b", "e", "a", "f", "c"]
)
print(test2)
"""
SPECS:
TIME COMPLEXITY: O(n ^ 2)
SPACE COMPLEXITY: O(n)
"""