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

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

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

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

Input

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

Output

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

Sample Input

ABDCEF
BDAECF

Sample Output

DBEFCA


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
    char num;
    struct node *l,*r;
};

struct node *kk(char i[],int n,char j[])
{
    if(n<=0)
        return NULL;
    struct node *root;
    root=(struct node *)malloc(sizeof(struct node));
    root->num=i[0];
    int v;
    for(v=0;v<n;v++)
    if(j[v]==i[0])break;
    root->l=kk(i+1,v,j);
    root->r=kk(i+v+1,n-1-v,j+v+1);
    return root;
};

void zz(struct node *head)
{
    if(head==NULL)
        return ;
    zz(head->l);
    zz(head->r);
    printf("%c",head->num);
}
int main()
{
    int a;
    char i[1000],j[1000];
    struct node *head;
    gets(i);
    gets(j);
    a=strlen(i);
    head=kk(i,a,j);
    zz(head);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/the_city_of_the__sky/article/details/80542402