2abcbacabdecdbeacExample Output
abcabcde#include<stdio.h>#include<string.h>#include<stdlib.h>#define maxsize 50typedef struct node{ char data; struct node *lc,*rc;}bitree;bitree * queue[51];int front=0,rear=0;bitree * create(int zlen,char qst[],char zst[]){ if(zlen<=0) return NULL; int i; bitree *t; t=(bitree *)malloc(sizeof(bitree)); t->data=qst[0]; for(i=0;i<zlen;i++) { if(zst[i]==qst[0]) break; } t->lc=create(i,qst+1,zst); t->rc=create(zlen-i-1,qst+i+1,zst+i+1); return t;}void enter_queue(bitree *t){ if((rear+1)%maxsize!=front) { rear=(rear+1)%maxsize; queue[rear]=t; }}bitree *delete_queue(){ if(rear!=front) { front=(front+1)%maxsize; return queue[front]; }}void level_order(bitree *t){ bitree *p; if(t) { enter_queue(t); while(rear!=front) { p=delete_queue(); printf("%c",p->data); if(p->lc) { enter_queue(p->lc); } if(p->rc) { enter_queue(p->rc); } } }}int main(){ int zlen,t; char qst[51],zst[51]; bitree * tree; scanf("%d",&t); while(t--) { scanf("%s%s",qst,zst); zlen=strlen(zst); tree=create(zlen,qst,zst); level_order(tree); printf("/n"); } return 0;}
新闻热点
疑难解答