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

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

數據結構實驗之二叉樹四:還原二叉樹

2019-11-10 20:23:36
字體:
來源:轉載
供稿:網友

PRoblem Description

給定一棵二叉樹的先序遍歷序列和中序遍歷序列,要求計算該二叉樹的高度。

Input

輸入數據有多組,每組數據第一行輸入1個正整數N(1 <= N <= 50)為樹中結點總數,隨后2行先后給出先序和中序遍歷序列,均是長度為N的不包含重復英文字母(區分大小寫)的字符串。

Output

輸出一個整數,即該二叉樹的高度。

Example Input

9 ABDFGHIECFDHGIBEAC

Example Output

5
 
#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct node{    char data;    struct node *lc,*rc;}bitree;int max;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(qst[0]==zst[i])            break;    }    t->lc=create(i,qst+1,zst);    t->rc=create(zlen-i-1,qst+i+1,zst+i+1);    return t;}void preshow(int count,bitree *t){    int k;    if(t)    {        if(count==0)            count=1;        k=count;        if(k>max)            max=k;        preshow(++count,t->lc);        preshow(++k,t->rc);    }}int main(){    int zlen;    char qst[51],zst[51];    bitree * tree;    while(scanf("%d",&zlen)!=EOF)    {        max=0;        scanf("%s%s",qst,zst);        zlen=strlen(zst);        tree=create(zlen,qst,zst);        preshow(0,tree);        printf("%d/n",max);    }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 策勒县| 通许县| 鲁山县| 平遥县| 全椒县| 板桥市| 栾川县| 远安县| 隆化县| 灵寿县| 定襄县| 安图县| 西乌珠穆沁旗| 盐亭县| 新邵县| 响水县| 滦南县| 桃园县| 宜君县| 平陆县| 治县。| 高雄市| 南平市| 沐川县| 三亚市| 云梦县| 竹溪县| 衡阳县| 和静县| 那曲县| 涿鹿县| 肇源县| 固安县| 柯坪县| 大冶市| 正镶白旗| 罗江县| 微博| 互助| 张掖市| 新宾|