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

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

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

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

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);    }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳西县| 辽源市| 双城市| 凯里市| 威海市| 睢宁县| 穆棱市| 金塔县| 茶陵县| 桓台县| 南充市| 丰原市| 陈巴尔虎旗| 博爱县| 宁河县| 龙口市| 神池县| 胶南市| 汽车| 专栏| 衡阳市| 永清县| 鲁甸县| 河南省| 寻乌县| 长白| 株洲县| 淳安县| 扎赉特旗| 增城市| 姚安县| 榆树市| 正阳县| 城口县| 婺源县| 宁夏| 潞西市| 获嘉县| 财经| 乌兰浩特市| 板桥市|