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

首頁 > 學院 > 開發設計 > 正文

求二叉樹的層次遍歷

2019-11-10 19:51:25
字體:
來源:轉載
供稿:網友

PRoblem Description

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

Input

輸入數據有多組,輸入T,代表有T組測試數據。每組數據有兩個長度小于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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 温宿县| 甘谷县| 贡嘎县| 娱乐| 阿荣旗| 关岭| 秦安县| 舞阳县| 万安县| 泽库县| 洞口县| 南昌县| 汉沽区| 太康县| 兴文县| 武清区| 海兴县| 洪江市| 平罗县| 东辽县| 涡阳县| 武平县| 邹城市| 射洪县| 若羌县| 镇宁| 和平县| 镇平县| 衡水市| 都江堰市| 九龙坡区| 丰顺县| 南通市| 辽源市| 盐津县| 大丰市| 都江堰市| 郧西县| 龙陵县| 江源县| 阿城市|