-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0106-Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal.cpp
More file actions
180 lines (167 loc) · 5.34 KB
/
0106-Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal.cpp
File metadata and controls
180 lines (167 loc) · 5.34 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************************************
* 0106-Construct_Binary_Tree_from_Inorder_and_Postorder_Traversal.cpp
* Billy.Ljm
* 16 Mar 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
* Given two integer arrays inorder and postorder where inorder is the inorder
* traversal of a binary tree and postorder is the postorder traversal of the
* same tree, construct and return the binary tree.
*
* ===========
* My Approach
* ===========
* The last node in postorder traversal is the root of the tree. We can then
* lookup this root in inorder traversal to find the elements to the left and
* right of the root. This can be repeated recursively to find the ordering of
* the binary tree from the root to the leaves.
*
* At each recursion, the relevant portions of the inorder traversal will be
* [left, root, right], and the postorder traversal will be [left, right, root].
* We will have to calculate the indices of each portion appropriately.
*
* Note that the ordering can be indeterminate if there are multiple nodes with
* identical values, since there are multiple possible roots. However, we will
* not account for this scenario here. Assuming no duplicate nodes, we can then
* use a hashtable for O(1) lookup of the root in the inorder traversal.
*
* This has a time complexity of O(n) to recurse through all nodes, and a space
* complexity of O(n) to store the map and to recurse through an unbalanced tree.
******************************************************************************/
#include <iostream>
#include <vector>
#include <unordered_map>
/**
* Definition for a binary tree node.
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
right(right) {}
};
class Solution {
public:
/**
* Builds binary tree from inorder and postorder traversal
*
* @param inorder nodes of the binary tree in inorder traversal
* @param postorder nodes of the binary tree in postorder traversal
*
* @return root of the corresponding binary tree
*/
TreeNode* buildTree(std::vector<int>& inorder, std::vector<int>& postorder) {
// build hashmap for inorder as value->index
std::unordered_map<int, int> inordermap (inorder.size());
for (int i = 0; i < inorder.size(); i++) {
inordermap[inorder[i]] = i;
}
return recurse(inordermap, postorder, 0, inorder.size() - 1, 0,
postorder.size() - 1);
}
private:
/**
* Recursively builds binary tree from inorder and postorder traversal
*
* @param inorder nodes of the binary tree in inorder traversal
* @param postorder nodes of the binary tree in postorder traversal
* @param mini index of the first node in the inorder traversal
* @param maxi index of the last node in the inorder traversal
* @param minp index of the first node in the postorder traversal
* @param maxp index of the last node in the postorder traversal
*
* @return root of the corresponding binary tree
*/
TreeNode* recurse(std::unordered_map<int, int>& inorder,
std::vector<int>& postorder, int mini, int maxi, int minp,
int maxp) {
// trisect the ranges
int rootval = postorder[maxp];
int midi = inorder[rootval];
maxp--;
int midp = maxp - (maxi - midi);
// construct binary tree
if (maxi - mini <= 0) { // if at leaf
return new TreeNode(rootval);
}
else if (maxi - midi <= 0) { // if no right subtree
return new TreeNode(rootval,
recurse(inorder, postorder, mini, midi - 1, minp, midp), nullptr
);
}
else if (midi - mini <= 0) { // if no left subtree
return new TreeNode(rootval, nullptr,
recurse(inorder, postorder, midi + 1, maxi, midp + 1, maxp)
);
}
else { // else recurse both subtrees
return new TreeNode(rootval,
recurse(inorder, postorder, mini, midi - 1, minp, midp),
recurse(inorder, postorder, midi + 1, maxi, midp + 1, maxp)
);
}
}
};
/**
* Deletes a binary tree
*
* @param root root of binary tree to be deleted
*/
void deleteTree(TreeNode* root) {
if (root == nullptr) return;
deleteTree(root->left);
deleteTree(root->right);
delete root;
}
/**
* Prints a binary tree in preorder traversal
*/
void printPreorder(TreeNode* root) {
if (root == nullptr) return;
std::cout << root->val << " ";
printPreorder(root->left);
printPreorder(root->right);
}
/**
* Prints a vector
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
for (auto i = vec.begin(); i != vec.end(); i++) {
os << *i << " ";
}
return os;
}
/**
* Test cases
*/
int main(void) {
Solution sol;
std::vector<int> inorder, postorder;
TreeNode* root;
// test case 1
inorder = {9,3,15,20,7};
postorder = {9,15,7,20,3};
root = sol.buildTree(inorder, postorder);
std::cout << "Inorder: " << inorder << std::endl;
std::cout << "Postorder: " << postorder << std::endl;
std::cout << "Preorder: ";
printPreorder(root);
std::cout << std::endl;
deleteTree(root);
// test case 2
inorder = { 2,1 };
postorder = { 2,1 };
root = sol.buildTree(inorder, postorder);
std::cout << "Inorder: " << inorder << std::endl;
std::cout << "Postorder: " << postorder << std::endl;
std::cout << "Preorder: ";
printPreorder(root);
deleteTree(root);
}