HEU 1046 Tree Recovery
1
/**************************************
2
Problem: HEU 1046 Tree Recovery
3
Time: 0.0010 s
4
Memory: 240 k
5
Accepted Time: 2009-03-25 14:37:24
6
Tips: 已知二叉树的先,中序,求后序。
7
**************************************/
8
#include <stdio.h>
9
#include <string.h>
10
struct Node
11
{
12
char c;
13
Node *left;
14
Node *right;
15
}tree[30];
16
char a[30],b[30];
17
int count1,count2,len;
18
void postord(Node *ptr)
19
{
20
if(ptr!=NULL)
21
{
22
postord(ptr->left);
23
postord(ptr->right);
24
printf("%c",ptr->c);
25
}
26
}
27
void fun(Node *ptr,int start,int end,int sign)
28
{
29
for(int i=start;i<=end;i++)
30
{
31
if(a[count1]==b[i])
32
{
33
count1++;
34
count2++;
35
int temp=count2;
36
tree[count2].c=b[i];
37
if(ptr!=NULL)
38
{
39
if(sign==1)ptr->left=&tree[temp];
40
else ptr->right=&tree[temp];
41
}
42
fun(&tree[temp],start,i-1,1);
43
fun(&tree[temp],i+1,end,-1);
44
break;
45
}
46
}
47
}
48
49
int main()
50
{
51
while(scanf("%s%s",a,b)!=EOF)
52
{
53
len=strlen(a);
54
count1=0;
55
count2=-1;
56
for(int i=0;i<len;i++)tree[i].left=tree[i].right=NULL;
57
fun(NULL,0,len-1,1);
58
postord(&tree[0]);
59
printf("\n");
60
}
61
return 0;
62
}
63
/**************************************2
Problem: HEU 1046 Tree Recovery3
Time: 0.0010 s4
Memory: 240 k 5
Accepted Time: 2009-03-25 14:37:246
Tips: 已知二叉树的先,中序,求后序。 7
**************************************/8
#include <stdio.h>9
#include <string.h>10
struct Node11
{12
char c;13
Node *left;14
Node *right;15
}tree[30];16
char a[30],b[30];17
int count1,count2,len;18
void postord(Node *ptr)19
{20
if(ptr!=NULL)21
{22
postord(ptr->left);23
postord(ptr->right);24
printf("%c",ptr->c);25
}26
}27
void fun(Node *ptr,int start,int end,int sign)28
{29
for(int i=start;i<=end;i++)30
{31
if(a[count1]==b[i])32
{33
count1++;34
count2++;35
int temp=count2;36
tree[count2].c=b[i];37
if(ptr!=NULL)38
{39
if(sign==1)ptr->left=&tree[temp];40
else ptr->right=&tree[temp];41
}42
fun(&tree[temp],start,i-1,1);43
fun(&tree[temp],i+1,end,-1);44
break;45
}46
}47
}48

49
int main()50
{51
while(scanf("%s%s",a,b)!=EOF)52
{53
len=strlen(a);54
count1=0;55
count2=-1;56
for(int i=0;i<len;i++)tree[i].left=tree[i].right=NULL;57
fun(NULL,0,len-1,1);58
postord(&tree[0]);59
printf("\n");60
}61
return 0;62
}63




浙公网安备 33010602011771号