-
Notifications
You must be signed in to change notification settings - Fork 9
/
BuildTree.cpp
61 lines (48 loc) · 2.12 KB
/
BuildTree.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
// Problem: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
#include<vector>
using namespace std;
// 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 BuildTree {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
// Step-I: Build inorder index map.
unordered_map<int, int> inorder_map;
int preorder_index = 0;
for (int i = 0; i < inorder.size(); ++i) {
inorder_map[inorder.at(i)] = i;
}
// Step-II: Recursively build the tree.
return buildTreeInt(preorder, inorder, inorder_map, preorder_index, 0, inorder.size() - 1);
}
TreeNode* buildTreeInt(const vector<int>& preorder, const vector<int>& inorder,
const unordered_map<int, int>& inorder_map,
int& preorder_index, int start_inorder, int end_inorder) {
// Base Case: No more roots left.
if (preorder.size() == preorder_index) return nullptr;
// Step-I: Find root.
int root_val = preorder.at(preorder_index++);
TreeNode* root = new TreeNode(root_val);
// Step-II: Get index in inorder.
int index = inorder_map.at(root_val);
if ((index < start_inorder) || (index > end_inorder)) {
return nullptr;
}
// Step-III: Left subtree = [start_inorder, index - 1].
if (start_inorder <= (index - 1)) {
root->left = buildTreeInt(preorder, inorder, inorder_map, preorder_index, start_inorder, index - 1);
}
// Step-IV: Right subtree = [index + 1, end_inorder].
if (end_inorder >= (index + 1)) {
root->right = buildTreeInt(preorder, inorder, inorder_map, preorder_index, index + 1, end_inorder);
}
return root;
}
};