题目描述
给出一棵二叉树的中序与后序排列。求出它的先序排列。(约定树结点用不同的大写字母表示,长度≤8 \le 8≤8)。
输入输出格式
输入格式:
222行,均为大写字母组成的字符串,表示一棵二叉树的中序与后序排列。
输出格式:
111行,表示一棵二叉树的先序。
输入输出样例
输入样例#1:
BADC
BDCA
输出样例#1:ABCD
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct tree {
char data;
struct tree *left,*right;
};
char a[1000],b[1000];
struct tree *creat(int x,int y,int n)
{
if(n<=0)
return NULL;
int i,j,k;
struct tree *T;
T=(struct tree *)malloc(sizeof(struct tree));
char root=b[n+y-1];
T->data=root;
// printf("%c ",root);
for(i=0;a[x+i]!=root;i++);
T->left=creat(x,y,i);
T->right=creat(x+i+1,y+i,n-i-1);
return T;
}
void houxubianli(struct tree *head)
{
if(head==NULL) return ;
printf("%c",head->data);
houxubianli(head->left);
houxubianli(head->right);
}
int main()
{
int i,j,n,m;
scanf("%s",a);
scanf("%s",b);
n=strlen(a);
struct tree *T=creat(0,0,n);
houxubianli(T);
}