您的位置:首页 > 理论基础 > 数据结构算法

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

2017-03-16 17:34 525 查看
数据结构上机测试4.1:二叉树的遍历与应用1

Time Limit: 1000MS Memory Limit: 65536KB

Submit Statistic

Problem Description

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

Input

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

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

Output

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

Example Input

ABDCEF

BDAECF

Example Output

DBEFCA

Hint

Author

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct no
{
int data;
struct no *lchild;
struct no *rchild;
}node;

node *create(char *a,char *b,int n)
{
if(n==0)
return NULL;
node *root;
char *p;
root = (node *)malloc(sizeof(node));
root->data = a[0];
for(p=b;(*p)!='\0';p++)
{
if(*p==a[0])
{
break;
}
}
int t;
t = p-b;
root->lchild = create(a+1,b,t);
root->rchild = create(a+1+t,p+1,n-t-1);
return root;
}

void visit(node *root)
{
if(root!=NULL)
{
visit(root->lchild);
visit(root->rchild);
printf("%c",root->data);
}
}
int main()
{
char a[1000],b[1000];
int n;
scanf("%s",a);
scanf("%s",b);
n = strlen(a);
node *root;
root = create(a,b,n);
visit(root);
}

/***************************************************
User name:
Result: Accepted
Take time: 0ms
Take Memory: 104KB
Submit time: 2017-03-16 08:40:27
****************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 二叉树