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

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

求二叉樹的層次遍歷

2019-11-10 20:24:26
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

PRoblem Description

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

Input

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

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ā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东台市| 东丰县| 嘉峪关市| 长沙县| 隆化县| 广德县| 仁化县| 宜春市| 昂仁县| 保亭| 东阿县| 汾西县| 三门县| 普安县| 延长县| 阳西县| 溆浦县| 苍山县| 大同市| 汶上县| 南平市| 土默特左旗| 轮台县| 兴山县| 剑河县| 灵宝市| 岑溪市| 太原市| 红安县| 三都| 集安市| 信阳市| 新巴尔虎右旗| 黄平县| 连云港市| 萨迦县| 扬州市| 黔江区| 肇州县| 横峰县| 雅江县|