|
21 | 21 | Output: 8 4 9 2 5 1 6 3 7 |
22 | 22 | """ |
23 | 23 |
|
| 24 | + |
24 | 25 | class TreeNode: |
25 | 26 |
|
26 | | - def __init__(self, val, left = None, right = None): |
| 27 | + def __init__(self, val, left=None, right=None): |
27 | 28 | self.val = val |
28 | 29 | self.left = left |
29 | 30 | self.right = right |
30 | 31 |
|
| 32 | + |
31 | 33 | pre_index = 0 |
32 | | - |
| 34 | + |
| 35 | + |
33 | 36 | def construct_tree_util(pre: list, post: list, low: int, high: int, size: int): |
34 | 37 | """ |
35 | | - Recursive function that constructs tree from preorder and postorder array. |
36 | | - |
37 | | - preIndex is a global variable that keeps track of the index in preorder |
38 | | - array. |
39 | | - preorder and postorder array are represented are pre[] and post[] respectively. |
40 | | - low and high are the indices for the postorder array. |
| 38 | + Recursive function that constructs tree from preorder and postorder array. |
| 39 | +
|
| 40 | + preIndex is a global variable that keeps track of the index in preorder |
| 41 | + array. |
| 42 | + preorder and postorder array are represented are pre[] and post[] respectively. |
| 43 | + low and high are the indices for the postorder array. |
41 | 44 | """ |
42 | 45 |
|
43 | 46 | global pre_index |
44 | 47 |
|
45 | 48 | if pre_index == -1: |
46 | 49 | pre_index = 0 |
47 | | - |
48 | | - |
49 | | - #Base case |
50 | | - if(pre_index >= size or low > high): |
| 50 | + |
| 51 | + # Base case |
| 52 | + if pre_index >= size or low > high: |
51 | 53 | return None |
52 | 54 |
|
53 | 55 | root = TreeNode(pre[pre_index]) |
54 | 56 | pre_index += 1 |
55 | 57 |
|
56 | | - #If only one element in the subarray return root |
57 | | - if(low == high or pre_index >= size): |
| 58 | + # If only one element in the subarray return root |
| 59 | + if low == high or pre_index >= size: |
58 | 60 | return root |
59 | 61 |
|
60 | | - #Find the next element of pre[] in post[] |
| 62 | + # Find the next element of pre[] in post[] |
61 | 63 | i = low |
62 | 64 | while i <= high: |
63 | | - if(pre[pre_index] == post[i]): |
| 65 | + if pre[pre_index] == post[i]: |
64 | 66 | break |
65 | 67 |
|
66 | 68 | i += 1 |
67 | 69 |
|
68 | | - #Use index of element present in postorder to divide postorder array |
69 | | - #to two parts: left subtree and right subtree |
70 | | - if(i <= high): |
| 70 | + # Use index of element present in postorder to divide postorder array |
| 71 | + # to two parts: left subtree and right subtree |
| 72 | + if i <= high: |
71 | 73 | root.left = construct_tree_util(pre, post, low, i, size) |
72 | | - root.right = construct_tree_util(pre, post, i+1, high, size) |
| 74 | + root.right = construct_tree_util(pre, post, i + 1, high, size) |
73 | 75 |
|
74 | 76 | return root |
75 | 77 |
|
76 | 78 |
|
77 | 79 | def construct_tree(pre: list, post: list, size: int): |
78 | 80 | """ |
79 | | - Main Function that will construct the full binary tree from given preorder |
80 | | - and postorder array. |
| 81 | + Main Function that will construct the full binary tree from given preorder |
| 82 | + and postorder array. |
81 | 83 | """ |
82 | 84 |
|
83 | | - global pre_index |
84 | | - root = construct_tree_util(pre, post, 0, size-1, size) |
| 85 | + root = construct_tree_util(pre, post, 0, size - 1, size) |
85 | 86 |
|
86 | 87 | return print_inorder(root) |
87 | 88 |
|
88 | 89 |
|
89 | | - |
90 | | -def print_inorder(root: TreeNode, result = None): |
| 90 | +def print_inorder(root: TreeNode, result=None): |
91 | 91 | """ |
92 | | - Prints the tree constructed in inorder format |
| 92 | + Prints the tree constructed in inorder format |
93 | 93 | """ |
94 | 94 | if root is None: |
95 | 95 | return [] |
96 | | - if result is None: |
| 96 | + if result is None: |
97 | 97 | result = [] |
98 | | - |
| 98 | + |
99 | 99 | print_inorder(root.left, result) |
100 | 100 | result.append(root.val) |
101 | 101 | print_inorder(root.right, result) |
102 | 102 | return result |
103 | 103 |
|
104 | | -if __name__ == '__main__': |
| 104 | + |
| 105 | +if __name__ == "__main__": |
105 | 106 | pre = [1, 2, 4, 5, 3, 6, 7] |
106 | 107 | post = [4, 5, 2, 6, 7, 3, 1] |
107 | 108 | size = len(pre) |
|
0 commit comments