USACO3.42American Heritage(二叉树)

已知中前 求后序

递归一下 有一些小细节

 1 /*
 2     ID: shangca2
 3     LANG: C++
 4     TASK: heritage
 5  */
 6 #include <iostream>
 7 #include<cstdio>
 8 #include<cstring>
 9 #include<algorithm>
10 #include<stdlib.h>
11 using namespace std;
12 char s1[30],s2[30];
13 int o,kk;
14 void order(int s,int e)
15 {
16     int i,k;
17     if(s>=kk)
18     return ;
19     if(s>=e)
20     return ;
21     if(e-s==1)
22     {
23         cout<<s1[s];
24         return ;
25     }
26     for( i = s ; i < e ; i++)
27     {
28         if(s1[i]==s2[o])
29         break;
30     }
31     o++;
32     order(s,i);
33     order(i+1,e);
34     if(i<e)
35     cout<<s1[i];
36 
37 }
38 int main()
39 {
40     freopen("heritage.in","r",stdin);
41     freopen("heritage.out","w",stdout);
42     cin>>s1>>s2;
43     o=0;
44     kk = strlen(s1);
45     order(0,kk);
46     puts("");
47     return 0;
48 }
View Code
原文地址:https://www.cnblogs.com/shangyu/p/3273486.html