-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstructBinaryTreefromInorderandPostorderTraversal.cpp
55 lines (53 loc) · 1.4 KB
/
ConstructBinaryTreefromInorderandPostorderTraversal.cpp
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
#include <bits/stdc++.h>
using namespace std;
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) {}
};
TreeNode *solve(int l, int r, int &pidx, unordered_map<int, int> &m, vector<int> &inorder, vector<int> &postorder)
{
if (l > r)
return nullptr;
int idx = m[postorder[pidx]];
TreeNode *root = new TreeNode(postorder[pidx]);
pidx--;
root->right = solve(idx + 1, r, pidx, m, inorder, postorder);
root->left = solve(l, idx - 1, pidx, m, inorder, postorder);
return root;
}
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
if (inorder.size() != postorder.size())
return nullptr;
unordered_map<int, int> m;
int x = 0;
for (auto i : inorder)
{
m[i] = x;
x++;
}
int pidx = postorder.size() - 1;
return solve(0, inorder.size() - 1, pidx, m, inorder, postorder);
}
void inorderT(TreeNode *root)
{
if (!root)
{
return;
}
inorderT(root->left);
cout << root->val << " ";
inorderT(root->right);
}
int main()
{
vector<int> inorder = {9, 3, 15, 20, 7};
vector<int> postorder = {9, 15, 7, 20, 3};
TreeNode *ans = buildTree(inorder, postorder);
inorderT(ans);
}