国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 射阳县| 平阳县| 南部县| 临洮县| 牙克石市| 灵川县| 武定县| 凌云县| 上虞市| 西和县| 桐梓县| 洛扎县| 松潘县| 江津市| 望城县| 宝鸡市| 富顺县| 西充县| 宜兴市| 四子王旗| 自贡市| 锡林浩特市| 贵港市| 太仓市| 胶南市| 邻水| 大姚县| 千阳县| 贺州市| 兴宁市| 平昌县| 佛冈县| 五大连池市| 田阳县| 河南省| 大港区| 奉新县| 灵山县| 奉新县| 德钦县| 那坡县|