-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
construct_tree_postorder_preorder.py
111 lines (85 loc) · 2.91 KB
/
construct_tree_postorder_preorder.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
"""
Given two arrays representing preorder and postorder traversal of a full
binary tree, construct the binary tree and print the inorder traversal of the
tree.
A full binary tree has either 0 or 2 children.
Algorithm:
1. Assign the first element of preorder array as root of the tree.
2. Find the same element in the postorder array and divide the postorder
array into left and right subtree.
3. Repeat the above steps for all the elements and construct the tree.
Eg: pre[] = {1, 2, 4, 8, 9, 5, 3, 6, 7}
post[] = {8, 9, 4, 5, 2, 6, 7, 3, 1}
Tree:
1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9
Output: 8 4 9 2 5 1 6 3 7
"""
class TreeNode:
def __init__(self, val, left = None, right = None):
self.val = val
self.left = left
self.right = right
pre_index = 0
def construct_tree_util(pre: list, post: list, low: int, high: int, size: int):
"""
Recursive function that constructs tree from preorder and postorder array.
preIndex is a global variable that keeps track of the index in preorder
array.
preorder and postorder array are represented are pre[] and post[] respectively.
low and high are the indices for the postorder array.
"""
global pre_index
if pre_index == -1:
pre_index = 0
#Base case
if(pre_index >= size or low > high):
return None
root = TreeNode(pre[pre_index])
pre_index += 1
#If only one element in the subarray return root
if(low == high or pre_index >= size):
return root
#Find the next element of pre[] in post[]
i = low
while i <= high:
if(pre[pre_index] == post[i]):
break
i += 1
#Use index of element present in postorder to divide postorder array
#to two parts: left subtree and right subtree
if(i <= high):
root.left = construct_tree_util(pre, post, low, i, size)
root.right = construct_tree_util(pre, post, i+1, high, size)
return root
def construct_tree(pre: list, post: list, size: int):
"""
Main Function that will construct the full binary tree from given preorder
and postorder array.
"""
global pre_index
root = construct_tree_util(pre, post, 0, size-1, size)
return print_inorder(root)
def print_inorder(root: TreeNode, result = None):
"""
Prints the tree constructed in inorder format
"""
if root is None:
return []
if result is None:
result = []
print_inorder(root.left, result)
result.append(root.val)
print_inorder(root.right, result)
return result
if __name__ == '__main__':
pre = [1, 2, 4, 5, 3, 6, 7]
post = [4, 5, 2, 6, 7, 3, 1]
size = len(pre)
result = construct_tree(pre, post, size)
print(result)