输入 先序和中序 输出后序
1 #include "bits/stdc++.h"
2
3 using namespace std;
4
5
6 struct aa
7 {
8 char date;
9 aa *ls,*rs;
10
11 };
12
13
14 aa *creat(int len,char xx[100],char zx[100] )
15 {
16 if(len<=0)return 0;
17 aa *t=(aa *)malloc(sizeof (aa));
18 t->date=xx[0];
19 int i;
20 for ( i=0;i<len;i++)
21 if(zx[i]==xx[0])break;
22 t->ls=creat(i,xx+1,zx);
23 t->rs=creat(len-i-1,xx+i+1,zx+i+1);
24 return t;
25
26
27 }
28
29 void show(aa *t)
30 {
31 if(t==0)return ;
32 show(t->ls);
33 show(t->rs);
34 cout<<t->date;
35
36 }
37
38 int main()
39 {
40 char zx[100],xx[100];
41 cin>>xx>>zx;
42 aa *root;root=creat(strlen(xx),xx,zx);
43 // cout<<"suc";
44 show(root);
45
46 }