-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconstructTree.cpp
More file actions
53 lines (41 loc) · 1.16 KB
/
reconstructTree.cpp
File metadata and controls
53 lines (41 loc) · 1.16 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
41
42
43
44
45
46
47
48
49
50
51
52
53
struct node {
int data;
struct node* left;
struct node* right;
};
void inOrder(struct node* root){
if (root == null) return;
inOrder(root->left);
cout<<root->data;
inOrder(root->right);
}
struct node* newNode(int data){
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* reconstructTree(vector<int>& pre, int preStart, int preEnd,
vector<int>& in, int inStart, int inEnd){
if (preStart > preEnd) return null;
if (preStart == preEnd) {
return newNode(pre[preStart]);
}
int rootIndex = 0;
for (int i = inStart; i <= inEnd; ++i){
if (in[i] = pre[preStart]) {
rootIndex = i;
break;
}
}
//1 2 3 4 5 r = 2
int numLeftTree = rootIndex - instart;
int numRightTree = inEnd - rootIndex;
struct node* root = newNode(inStart[rootIndex]);
root->left = reconstructTree(pre, preStart+1, preStart+numLeftTree,
in, inStart, inStart+numLeftTree-1);
root->right = reconstructTree(pre, preStart+1+numLeftTree+1, preEnd,
in, inStart+numLeftTree+1, inEnd);
return root;
}