直接由前序和中序求出后序序列
后序遍历是左右根
以pos为前序序列的遍历位置
每一次找出在中序序列in中的前序序列pre[pos]的位置 这个就是树根 即为i 以此划分为左右子树
所以在代码中可以看到左子树范围被化为 start到 i-1 右子树同理
这样就可以模拟后序序列的遍历过程
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
#include<string>
#define de cout<<"debug"<<endl;
using namespace std;
char pre[30],in[30],post[30];
int len,pos;
void Post(int start,int end)
{
if(end<start) return;
int i;
for(i=0;i<=end;i++)
{
if(pre[pos]==in[i]) break;
}
pos++;
Post(start,i-1);
Post(i+1,end);
printf("%c",in[i]);
}
int main()
{
while(scanf("%s%s",pre,in)!=EOF)
{
pos=0;
len=strlen(pre);
Post(0,len-1);
printf("\n");
}
return 0;
}