數據結構實驗之求二叉樹後序遍歷和層次遍歷
Time Limit: 1000ms Memory limit: 65536K 有疑問?點這裏^_^
題目描述
輸入
輸出
示例輸入
2 abdegcf dbgeafc xnliu lnixu
示例輸出
dgebfca abcdefg linux xnuli
提示
來源
示例程序
- #include<stdio.h>
- #include<string.h>
- char st1[100],st2[100];
- struct node
- {
- char c;
- struct node *lnext,*rnext;
- };
- void build(int n,char *s1,char *s2,struct node *&p)
- {
- if(n<=0)
- return ;
- p=new node;
- p->c=s1[0];
- int num=strchr(s2,s1[0])-s2;
- build(num,s1+1,s2,p->lnext);
- build(n-num-1,s1+num+1,s2+num+1,p->rnext);
- }
- void last(struct node *t)
- {
- if(t==NULL)
- return ;
- else
- {
- last(t->lnext);
- last(t->rnext);
- printf("%c",t->c);
- }
- }
- void cengci(struct node *t)
- {
- int s=0,e=0;
- struct node *que[100100],*now,*tmp;
- que[s++]=t;
- while(s>e)
- {
- now=que[e++];
- printf("%c",now->c);
- tmp=now;
- if(tmp->lnext!=NULL)
- que[s++]=tmp->lnext;
- if(tmp->rnext!=NULL)
- que[s++]=tmp->rnext;
- }
- }
- int main()
- {
- int n;
- scanf("%d%*c",&n);
- while(n--)
- {
- scanf("%s%s",st1,st2);
- int len=strlen(st1);
- struct node *q;
- build(len,st1,st2,q);
- last(q);
- printf("\n");
- cengci(q);
- printf("\n");
- }
- return 0;
- }