-
Notifications
You must be signed in to change notification settings - Fork 0
/
construct-binary-tree-from-preorder-and-inorder-traversal.cc
73 lines (59 loc) · 1.48 KB
/
construct-binary-tree-from-preorder-and-inorder-traversal.cc
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
// https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
#include <vector>
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode *buildTree(const vector<int> &preorder, int b1, int e1, const vector<int> &inorder, int b2, int e2) {
if (b1 > e1 || b2 > e2) {
return NULL;
}
int root_val = preorder[b1];
int i = b2;
while (i < e2 && inorder[i] != root_val) {
i++;
}
if (i > e2) {
return NULL;
}
TreeNode *root = new TreeNode(root_val);
root->left = buildTree(preorder, b1 + 1, b1 + i - b2, inorder, b2, i - 1);
root->right = buildTree(preorder, b1 + i - b2 + 1, e1, inorder, i + 1, e2);
return root;
}
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return buildTree(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
}
};
void inorder_traverse(TreeNode *r) {
if (r) {
inorder_traverse(r->left);
cout << r->val << " ";
inorder_traverse(r->right);
}
}
void postorder_traverse(TreeNode *r) {
if (r) {
inorder_traverse(r->left);
inorder_traverse(r->right);
cout << r->val << " ";
}
}
int main(int argc, char const* argv[])
{
vector<int> inorder{-1};
vector<int> postorder{-1};
Solution s;
TreeNode *root = s.buildTree(inorder, postorder);
inorder_traverse(root);
cout << endl;
postorder_traverse(root);
cout << endl;
return 0;
}