-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebuild_tree.c
executable file
·67 lines (60 loc) · 2.12 KB
/
rebuild_tree.c
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
#include <stdio.h>
#include <stdlib.h>
struct node{
int num;
struct node* pleft;
struct node* pright;
};
struct node* rebuild_process(int* pre_start, int* pre_end, int* in_start, int* in_end){
int value = pre_start[0];
struct node* root = (struct node*)malloc(sizeof(struct node));
root->num = value;
root->pleft = root->pright = NULL;
//judge whether only one node
if(pre_start == pre_end){
if(in_start == in_end && *pre_start == *in_start)
return root;
else
return NULL;
}
//already know the root to find index in the inorder arr
//the root_inorder show the root node addr in the inorder array
int* root_inorder = in_start;
while(root_inorder <= in_end && *root_inorder != value){
++root_inorder;
}
if(root_inorder == in_end && *root_inorder != value)
return NULL;
int left_len = root_inorder - in_start;
int* left_pre_end = pre_start + left_len;//the offset
//sperate the left and right child trees
if(left_len > 0){
//build left child tree
root->pleft = rebuild_process(pre_start+1,left_pre_end,in_start,root_inorder-1);
}
if(left_len < in_end - in_start){
//build right child tree
root->pright = rebuild_process(left_pre_end + 1,pre_end,root_inorder+1 ,in_end);
}
return root;
}
struct node* rebuild(int* preorder, int* inorder, int len){
if(preorder == NULL || inorder == NULL || len <=0)
return NULL;
return rebuild_process(preorder,preorder+len-1,inorder,inorder+len-1);
}
void show_last(struct node* root){
if(NULL == root)
return ;
show_last(root->pleft);
show_last(root->pright);
printf("%d\n",root->num);
return;
}
int main(){
int pre[8] = {1,2,4,7,3,5,6,8};
int in[8] = {4,7,2,1,5,3,8,6};
struct node* root = rebuild(pre,in,8);
show_last(root);
return 0;
}