-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path106.construct-binary-tree-from-inorder-and-postorder-traversal.cpp
51 lines (46 loc) · 1.5 KB
/
106.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
/*
* @lc app=leetcode id=106 lang=cpp
*
* [106] Construct Binary Tree from Inorder and Postorder Traversal
*/
// @lc code=start
/**
* 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
{
unordered_map<int, int> inorder_map; //{val, idx}
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
// make a map for inorder
const int n = inorder.size();
for (int i = 0; i < inorder.size(); ++i)
{
inorder_map[inorder[i]] = i;
}
return helper(inorder, postorder, 0, n - 1, 0, n - 1);
}
TreeNode *helper(vector<int> &inorder, vector<int> &postorder, int inSt, int inEnd, int postSt, int postEnd)
{
// base case
if (inSt > inEnd)
return nullptr;
// int the postorder, the last node int range is the root
int rootVal = postorder[postEnd];
TreeNode *root = new TreeNode(rootVal);
int pos = inorder_map[rootVal];
root->left = helper(inorder, postorder, inSt, pos - 1, postSt, postSt + pos - 1 - inSt);
root->right = helper(inorder, postorder, pos + 1, inEnd, postSt + pos - inSt, postEnd - 1);
return root;
}
};
// @lc code=end