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

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

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

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

Input

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

Output

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

Sample Input

ABDCEF
BDAECF

Sample Output

DBEFCA

Hint

扫描二维码关注公众号,回复: 2759784 查看本文章

Source


#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;
}

猜你喜欢

转载自blog.csdn.net/rangran/article/details/81637472