-
Notifications
You must be signed in to change notification settings - Fork 0
/
Construct Binary Tree from Inorder and Postorder Traversal.cpp
55 lines (45 loc) · 1.47 KB
/
Construct Binary Tree from Inorder and Postorder Traversal.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
/**
* 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) {}
* };
*/
#include <vector>
#include <unordered_map>
#include <function>
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) {}
};
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
{
unordered_map<int, int> pos;
for(int i = 0; i < inorder.size(); i++)
{
pos[inorder[i]] = i;
}
function<TreeNode*(int, int, int, int)> buildTree = [&](int is, int ie, int ps, int pe)
{
if(ps > pe) return (TreeNode*)nullptr;
int im = pos[postorder[pe]];
int pm = ps + (im - is) - 1;
auto root = new TreeNode(postorder[pe]);
root->left = buildTree(is, im - 1, ps, pm);
root->right = buildTree(im + 1, ie, pm + 1, pe - 1);
return root;
};
return buildTree(0, inorder.size() - 1, 0, postorder.size() - 1);
}
};