HEU 1046 Tree Recovery

 1/**************************************
 2Problem: HEU 1046 Tree Recovery
 3Time: 0.0010 s
 4Memory: 240 k 
 5Accepted Time: 2009-03-25 14:37:24
 6Tips: 已知二叉树的先,中序,求后序。 
 7**************************************/

 8#include <stdio.h>
 9#include <string.h>
10struct Node
11{
12    char c;
13    Node *left;
14    Node *right;
15}
tree[30];
16char a[30],b[30];
17int count1,count2,len;
18void postord(Node *ptr)
19{
20    if(ptr!=NULL)
21    {
22        postord(ptr->left);
23        postord(ptr->right);
24        printf("%c",ptr->c);
25    }

26}

27void 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
49int 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
posted @ 2009-04-02 12:02  主函数  阅读(327)  评论(2编辑  收藏  举报