国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

求二叉樹的層次遍歷

2019-11-10 20:15:15
字體:
供稿:網(wǎng)友

PRoblem Description

已知一顆二叉樹的前序遍歷和中序遍歷,求二叉樹的層次遍歷。

Input

輸入數(shù)據(jù)有多組,輸入T,代表有T組測試數(shù)據(jù)。每組數(shù)據(jù)有兩個長度小于50的字符串,第一個字符串為前序遍歷,第二個為中序遍歷。

Output

每組輸出這顆二叉樹的層次遍歷。

Example Input

2abcbacabdecdbeac

Example 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;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 竹溪县| 云林县| 鲁甸县| 龙州县| 茶陵县| 中方县| 汉中市| 平南县| 北票市| 泸水县| 广丰县| 邯郸市| 环江| 福海县| 都江堰市| 元江| 察隅县| 镇安县| 忻城县| 武清区| 马鞍山市| 安龙县| 天门市| 昌平区| 龙江县| 尖扎县| 海宁市| 玉树县| 宁河县| 洛川县| 五峰| 图片| 凉城县| 蓬安县| 孟津县| 海南省| 商南县| 凌海市| 西林县| 赫章县| 龙井市|