Input
第二行输入二叉树的中序遍历序列。
Output
Sample Input
ABDCEF BDAECF
Sample Output
DBEFCA
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char str1[100],str2[100];
struct node
{
char data;
struct node *l,*r;
};
struct node *build(char *str1,char *str2,int s)
{
int i;
struct node *root;
root=(struct node *)malloc(sizeof(struct node));
if(s<=0)
return NULL;
root->data=*str1;
for(i=0; i<s; i++)
{
if(str2[i]==*str1)
break;
}
root->l=build(str1+1,str2,i);
root->r=build(str1+i+1,str2+i+1,s-i-1);
return root;
}
void last(struct node *root)
{
if(root!=NULL)
{
last(root->l);
last(root->r);
printf("%c",root->data);
}
}
int main()
{
int st;
struct node *root;
scanf("%s",str1);
scanf("%s",str2);
st=strlen(str1);
root=build(str1,str2,st);
last(root);
return 0;
}