首页 > 学院 > 开发设计 > 正文

数据结构上机测试4.1:二叉树的遍历与应用1

2019-11-11 03:00:37
字体:
来源:转载
供稿:网友

PRoblem Description

输入二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列。

Input

第一行输入二叉树的先序遍历序列;第二行输入二叉树的中序遍历序列。

Output

输出该二叉树的后序遍历序列。

Example Input

ABDCEFBDAECF

Example Output

DBEFCA
 
#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct node{    char data;    struct node *lc,*rc;}bitree;bitree * create(int hlen,char qst[51],char hst[51]){    int i;    bitree * t;    if(hlen<=0)        return NULL;    t=(bitree *)malloc(sizeof(bitree));    t->data=qst[0];    for(i=0;i<hlen;i++)    {        if(hst[i]==qst[0])            break;    }    t->lc=create(i,qst+1,hst);    t->rc=create(hlen-i-1,qst+i+1,hst+i+1);    return t;}void postshow(bitree * tree){    bitree * t;    t=tree;    if(t)    {        postshow(t->lc);        postshow(t->rc);        printf("%c",t->data);    }}int main(){    int hlen;    char qst[51],hst[51];    bitree * tree;    scanf("%s%s",qst,hst);    hlen=strlen(hst);    tree=create(hlen,qst,hst);    postshow(tree);    printf("/n");    return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表