Problem Description
输入二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列。
Input
第一行输入二叉树的先序遍历序列;
第二行输入二叉树的中序遍历序列。
Output
输出该二叉树的后序遍历序列。
Sample Input
ABDCEF
BDAECF
Sample Output
DBEFCA
Hint
Source
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct tree
{
struct tree *left,*right;
int data;
};
struct tree *creat(int len,char a[],char b[]);
void backshow(struct tree *t);
int main()
{
int len;
char a[55],b[55];
struct tree *root;
scanf("%s %s",a,b);
len=strlen(a);
root=creat(len,a,b);
backshow(root);
printf("\n");
return 0;
}
struct tree *creat(int len,char a[],char b[])
{
if(len<=0)return NULL;
struct tree *t;
t=(struct tree *)malloc(sizeof(struct tree));
t->data=a[0];
int i;
for(i=0; i<len; i++)
{
if(a[0]==b[i])break;
}
t->left=creat(i,a+1,b);
t->right=creat(len-i-1,a+i+1,b+i+1);
return t;
}
void backshow(struct tree *t)
{
if(t==NULL) return;
backshow(t->left);
backshow(t->right);
printf("%c",t->data);
}