1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include<stdio.h> #include<string.h> typedef struct Tree{ char c; Tree *lc,*rc; }tree; int len_mid; tree *build(char ch) { tree *r=new tree; r->c=ch; r->lc=NULL; r->rc=NULL; return r; } char *find(char *s,char ch) { while(*s!=ch && *s!='\0') s++; return s; } void visit(tree *r) { if(r!=NULL) { printf("%c",r->c); visit(r->lc); visit(r->rc); } } void creat(tree **root,char *last,char *mid ,int len) { if(len==0) { *root=NULL; return ; } tree *r=build(last[len-1]); *root=r; char *p=find(mid,r->c); int leftlen=strlen(mid)-strlen(p); int rightlen=len-leftlen-1; creat(&(r->lc),last,mid,leftlen); creat(&(r->rc),last+leftlen,p+1,rightlen); } int main() { char last[30],mid[30]; while(~scanf("%s %s",last,mid)) { tree *root=NULL; len_mid=strlen(mid); creat(&root,last,mid,len_mid); visit(root); printf("\n"); } return 0; }
|