分析:类似上一篇博客
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=30;
struct Node
{
char data;
struct Node *left,*right;
};
struct Node node[maxn];
char xianxu[maxn],zhongxu[maxn];
int nodeindex=0;
struct Node *NewNode()
{
node[nodeindex].left=NULL;
node[nodeindex].right=NULL;
return &node[nodeindex++];
};
struct Node* CreateTree(char *xianxu,char *zhongxu,int len)//先和中建树
{
if (len==0) return NULL;
int k=0;
while (zhongxu[k]!=xianxu[0]) k++;
struct Node *h=NewNode();
h->data=xianxu[0];
h->left=CreateTree(xianxu+1,zhongxu,k);
h->right=CreateTree(xianxu+k+1,zhongxu+k+1,len-k-1);
return h;
};
void houbian(Node *root)//后序遍历
{
if (root)
{
houbian(root->left);
houbian(root->right);
printf("%c",root->data);
}
}
int main()
{
while (scanf("%s",xianxu)!=EOF)
{
nodeindex=0;
scanf("%s",zhongxu);
int len=strlen(xianxu);
struct Node *root=CreateTree(xianxu,zhongxu,len);
houbian(root);
printf("\n");
}
return 0;
}