#include<stdio.h>
#include<string>
using namespace std;
struct node
{
node *lchild;
node *rchild;
char c;
}tree[50];
int loc = 0;
node *creat()
{
tree[loc].lchild = tree[loc].rchild = NULL;
return &tree[loc++];
}
char str1[30], str2[30];
void houxu(node *t)
{
if (t->lchild != NULL)
houxu(t->lchild);
if (t->rchild != NULL)
houxu(t->rchild);
printf("%c", t->c);
}
node *build(int s1, int e1, int s2, int e2)
{
node *ret = creat();
ret->c = str1[s1];
int index;
for (int i = s2; i <= e2; i++)
{
if (str2[i] == str1[s1])
{
index = i; break;
}
}
if (index != s2)
ret->lchild = build(s1 + 1, s1 + (index - s2), s2, index - 1);
if (index != e2)
ret->rchild = build(s1 + (index - s2) + 1, e1, index + 1, e2);
return ret;
}
int main()
{
while (scanf("%s", str1) != EOF)
{
scanf("%s", str2);
int L1 = strlen(str1);
int L2 = strlen(str2);
node *t = build(0, L1 - 1, 0, L2 - 1);
houxu(t);
printf("\n");
}
return 0;
}
由先序遍历和中序遍历还原成原来的树,再对树进行后续遍历