-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindTree
64 lines (57 loc) · 1.32 KB
/
findTree
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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;;
const int N=31;
typedef struct BitNode
{
char value;
BitNode *lchild,*rchild;
}BitNode,*BiTree;
void CreatTree(BitNode* &root,char *pre,int l1,int r1,char *in,int l2,int r2)
{
if(l1<=r1&&l2<=r2)
{
int key=pre[l1];
int midIndex=-1;
for(int i=l2;i<=r2;i++)
{
if(in[i]==key)
{
midIndex=i;
break;
}
}
root=(BitNode *)malloc(sizeof(BitNode));
root->value=key;
root->lchild=NULL;
root->rchild=NULL;
int llen=midIndex-l2;
CreatTree(root->lchild, pre, l1+1, l1+llen, in, l2, midIndex-1);
CreatTree(root->rchild, pre, l1+llen+1, r1, in, midIndex+1, r2);
}
}
void postOrderTraverse(BitNode * &root)
{
if(root->lchild)
postOrderTraverse(root->lchild);
if(root->rchild)
postOrderTraverse(root->rchild);
printf("%c",root->value);
}
int main()
{
char pre[N],in[N];
while(scanf("%s",pre)!=EOF)
{
scanf("%s",in);
int len1=strlen(pre);
int len2=strlen(in);
BitNode *root=NULL;
CreatTree(root,pre,0,len1-1,in,0,len2-1);
postOrderTraverse(root);
printf("\n");
}
return 0;
}