Description
Given the mid-order traversal and post-order traversal of a binary tree, find the first-order traversal of the binary tree
Input
There are multiple sets of input data. The first line is an integer t (t<1000), which means there are t sets of test data. Each group includes two strings with a length less than 50. The first string represents the middle-order traversal sequence of the binary tree, and the second string represents the post-order traversal sequence of the binary tree.
Output
Output the pre-order traversal sequence of the binary tree
Sample
Input
2
dbgeafc
dgebfca
lnixu
linux
Output
abdegcf
xnliu
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char a[100],b[100];
struct node
{
char data;
struct node*lchild,*rchild;
};
struct node *creat(char *a,char*b,int len)
{
int i;
if(len==0)
return NULL;
struct node *root;
root=(struct node*)malloc(sizeof(struct node));
root->data=b[len-1];
for(i=0;i<len;i++)
{
if(b[len-1]==a[i])
break;
}
root->lchild=creat(a,b,i);
root->rchild=creat(a+i+1,b+i,len-i-1);
return root;
}
void front(struct node*root)
{
if(root)
{
printf("%c",root->data);
front(root->lchild);
front(root->rchild);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",a,b);
struct node*root;
root=(struct node*)malloc(sizeof(struct node));
int len=strlen(a);
root=creat(a,b,len);
front(root);
printf("\n");
}
return 0;
}