-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreConstructBinaryTree.cpp
More file actions
40 lines (32 loc) · 1.03 KB
/
reConstructBinaryTree.cpp
File metadata and controls
40 lines (32 loc) · 1.03 KB
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
/**
*输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
*假设输入的前序遍历和中序遍历的结果中都不含重复的数字
**/
class Solution {
public:
struct TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> in) {
if(pre.size() == 0 || in.size() == 0)
return NULL;
vector<int> pre_left,pre_right,in_left,in_right;
TreeNode* root = new TreeNode(pre[0]);
int i,j;
for(i = 0; i < in.size(); i++)
{
if(in[i] == pre[0])
break;
}
for(j = 0; j < i; j++)
{
in_left.push_back(in[j]);
pre_left.push_back(pre[j+1]);
}
for(j = i + 1; j < in.size(); j++)
{
in_right.push_back(in[j]);
pre_right.push_back(pre[j]);
}
root->left = reConstructBinaryTree(pre_left,in_left);
root->right = reConstructBinaryTree(pre_right,in_right);
return root;
}
};