-
Notifications
You must be signed in to change notification settings - Fork 0
/
reConstructBinaryTree.cpp
44 lines (43 loc) · 1.45 KB
/
reConstructBinaryTree.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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
int preLength = pre.size();
int vinLength = vin.size();
if(preLength <=0 || vinLength <= 0)
return NULL;
TreeNode* root = new TreeNode(pre[0]);
// 找到根节点在中序遍历中的位置,中序遍历的根节点的左边都是左子树,右边都是右子树
int rootPosInVin = 0;
for(int i = 0; i < vinLength; i++){
if(vin[i] == pre[0]){
rootPosInVin = i;
break;
}
}
// 左子树 && 右子树
vector<int> preLeft,preRight,vinLeft,vinRight;
// 找到左子树
for(int i = 0; i < rootPosInVin; i++){
preLeft.push_back(pre[i + 1]); //前序遍历的左子树,第一个是根节点
vinLeft.push_back(vin[i]); //中序遍历的左子树
}
// 找到右子树
for(int i = rootPosInVin + 1; i < vinLength; i++){
preRight.push_back(pre[i]);
vinRight.push_back(vin[i]);
}
// 递归
root->left = reConstructBinaryTree(preLeft,vinLeft);
root->right = reConstructBinaryTree(preRight,vinRight);
return root;
}
};