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

来源:互联网 发布:阿里云邮箱登陆入口 编辑:程序博客网 时间:2024/05/23 13:55
数据结构上机测试4.1:二叉树的遍历与应用1

Time Limit: 1000MS    Memory limit: 65536K

题目描述

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

输入

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

输出

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

示例输入

ABDCEFBDAECF

示例输出

DBEFCA

 

#include<stdio.h>#include<string.h>#include<malloc.h>#include<stdlib.h>typedef struct node{    char data;    struct node *lchild,*rchild;}*Tree;void CreateBitree(Tree &p){    char ch;    scanf("%c",&ch);    if(ch==',')        p=NULL;    else    {        p=new node;        p->data=ch;        CreateBitree(p->lchild);        CreateBitree(p->rchild);    }    return ;}void zpreorder(Tree p){    if(p)    {        zpreorder(p->lchild);        printf("%c",p->data);        zpreorder(p->rchild);    }}void hpreorder(Tree p){    if(p)    {        hpreorder(p->lchild);        hpreorder(p->rchild);        printf("%c",p->data);    }}void ycount(Tree p,int &count){    if(p)    {        if(p->lchild==NULL&&p->rchild==NULL)        {            count++;            return ;        }        if(p->lchild) ycount(p->lchild,count);        if(p->rchild) ycount(p->rchild,count);    }}int sleaf(Tree p){    int high=0;    if(!p) return high;    int n=sleaf(p->lchild);    int m=sleaf(p->rchild);    high=m>n?m:n;    return high+1;}int main(){    node *p;    CreateBitree(p);    zpreorder(p);    printf("\n");    hpreorder(p);    printf("\n");    int countleaf=0;    ycount(p,countleaf);    printf("%d\n",countleaf);    printf("%d\n",sleaf(p));    return 0;}


 

0 0