-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestoreTreeFromTwo.cpp
61 lines (56 loc) · 1.56 KB
/
restoreTreeFromTwo.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
#include <cstdio>
#include <vector>
#include <queue>
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(vector<int>& pre, vector<int>& in)
{
if (pre.size() == 0) return NULL;
TreeNode* root = new TreeNode(pre[0]);
if (pre.size() == 1) return root;
vector<int> leftIn, leftPre, rightIn, rightPre;
int location = 0;
while (in[location]!=pre[0])//pre0 is the rootnode
{
leftIn.push_back(in[location]);
++location;
}
for (size_t i = 1; i < location; ++i)
{
leftPre.push_back(pre[i]);
}
for (int i = location + 1; i<pre.size(); i++) {
rightPre.push_back(pre[i]);
rightIn.push_back(in[i]);
}
root->left = buildChild(leftPre, leftIn);
root->right = buildChild(rightPre, rightIn);
return root;
}
TreeNode* buildChild(vector<int> preorder, vector<int> inorder)
{
if (preorder.size() == 0) return NULL; //出口条件:preorder为空,则表示这个节点是NULL
TreeNode* root = new TreeNode(preorder[0]); //生成当前子树的根节点
vector<int> leftIn, leftPre, rightIn, rightPre;
int location=0;
while (inorder[location] != root->val) {
leftIn.push_back(inorder[location]);
location++;
}
for (int i = 1; i <= location; i++) leftPre.push_back(preorder[i]);
for (int i = location + 1; i<preorder.size(); i++) {
rightPre.push_back(preorder[i]);
rightIn.push_back(inorder[i]);
}
root->left = buildChild(leftPre, leftIn);
root->right = buildChild(rightPre, rightIn);
return root;
}
};