-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstruct Binary Tree from Preorder and Inorder Traversal.cpp
93 lines (87 loc) · 2.12 KB
/
Construct Binary Tree from Preorder and Inorder 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
/**
* Definition for a binary tree node.
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
struct RESULT {
int nmaxDepth;
int nmaxPath;
};
class Solution {
public:
bool gInvalid = false;
TreeNode *solve(vector<int> &preorder, int pbeg, int pend, vector<int> &inorder, int ibeg, int iend) {
if(pbeg > pend) {
return NULL;
}
if(pbeg == pend) {
return new TreeNode(preorder[pbeg]);
}
TreeNode *root = new TreeNode(preorder[pbeg]);
int cur = 0;
for(cur = ibeg; cur <= iend; cur++) {
if(root->val == inorder[cur]) {
break;
}
}
if(cur > iend) {
gInvalid = true;
return NULL;
}
int len = cur - ibeg;
root->left = solve(preorder, pbeg + 1, pbeg + len, inorder, ibeg, ibeg + len - 1);
root->right = solve(preorder, pbeg + len + 1, pend, inorder, ibeg + len + 1, iend);
return root;
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(preorder.size() != inorder.size() || preorder.empty() || inorder.empty()) {
return NULL;
}
int len = preorder.size();
TreeNode *res = solve(preorder, 0, len - 1, inorder, 0, len - 1);
if(gInvalid) {
return NULL;
}
return res;
}
};
TreeNode *buildTree(int depth, int &val) {
TreeNode *root = new TreeNode(val);
if(depth == 0) {
return root;
}
root->left = buildTree(depth - 1, ++val);
root->right = buildTree(depth - 1, ++val);
return root;
}
int main() {
int arr1[] = {1, 2, 3};
int arr2[] = {2, 1, 3};
vector<int> v1(arr1, arr1 + 2);
vector<int> v2(arr2, arr2 + 2);
Solution s;
TreeNode *res = s.buildTree(v1, v2);
queue<TreeNode*> q;
q.push(res);
while(!q.empty()) {
TreeNode *cur = q.front();
q.pop();
cout << cur->val << endl;
if(cur->left) {
q.push(cur->left);
}
if(cur->right) {
q.push(cur->right);
}
}
return 0;
}