-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
73 lines (64 loc) · 2.17 KB
/
Solution.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <algorithm>
#include <array>
#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
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) {}
};
using namespace std;
class Solution {
private:
TreeNode* build(vector<int> const& preorder,
vector<int> const& inorder,
unordered_map<int, int>& inorderIndex,
int& preIdx,
int const inStart,
int const inEnd) {
if (inStart > inEnd) {
return nullptr;
}
TreeNode* root = new TreeNode(preorder[preIdx]);
int inorderIdx = inorderIndex[preorder[preIdx]];
++preIdx; // Passed by reference so that the left-recursive call will
// update
root->left =
build(preorder, inorder, inorderIndex, preIdx, inStart, inorderIdx - 1);
root->right =
build(preorder, inorder, inorderIndex, preIdx, inorderIdx + 1, inEnd);
return root;
}
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
// preorder: root, left, right
// inorder: left, root, right
// Therefore, traverse the preorder. The corresponding left and right
// subtrees will be the left and right subarrays in the inorder.
// E.g. preorder = [3, 9, 20, 15, 7], inorder = [9, 3, 15, 20, 7]
// first iter: root = 3
// leftSubtree = [9], rightSubtree = [15, 20, 7].
// Recurse.
// Therefore, we will need the index of the node in the inorder traversal.
// An offset will also be needed to prevent copying of arrays. E.g.
// when recursing onto the rightSubtree, the offset will be 2.
// value : index map
unordered_map<int, int> inorderIndex;
for (int i = 0; i < inorder.size(); ++i) {
inorderIndex[inorder[i]] = i;
}
int idx = 0;
return build(preorder, inorder, inorderIndex, idx, 0, inorder.size() - 1);
}
};