poj2255(递归)

简单题

已知前序遍历,中序遍历,求后序遍历

思路,找到根,找到左子树,找到右子树

 1 #include <stdio.h>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 char a[30],b[30];
 6 int count;
 7 
 8 void  find(int start, int end)
 9 {
10     int i;
11     if(start>end)
12     {
13         return;
14     }
15     char root=a[count++];
16     for( i=start;i<=end;i++)
17     {
18         if(b[i]==root)
19         {
20             break;
21         }
22     }
23     find(start,i-1);
24     find(i+1,end);
25     //cout<<root;
26     printf("%c",root);
27 }
28 
29 
30 
31 int main()
32 {
33     while(scanf("%s %s",a,b)!=EOF)
34     {
35         count=0;
36         int len1;
37         len1=strlen(b);
38         find(0,len1-1);
39         printf("\n");
40     }
41     return 0;
42 }

如果输入的时候好像必须要scanf("%s %s",a,b)!=EOF,否则Output Limit Exceeded

 

posted on 2012-08-11 15:28  矮人狙击手!  阅读(255)  评论(0编辑  收藏  举报

导航