根据树的前序和中序遍历的结果得到它后序遍历的结果
注意递归函数的写法,控制好递归的边界就行了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<vector>
#include<algorithm>
#include<assert.h>
using namespace std;
string pre, in;
void build(int lx, int rx, int ly, int ry)
{
if (lx > rx || ly > ry) return;
char root = pre[lx];
int i;
for (i = ly; i <= ry; i++) if (in[i] == root) break;
int cnt = i - ly;
build(lx + 1, lx + cnt, ly, i - 1);
build(lx + cnt + 1, rx, i + 1, ry);
cout << root;
}
int main()
{
while (cin >> pre >> in)
{
int lena = pre.length();
int lenb = in.length();
build(0, lena - 1, 0, lenb - 1);
cout << endl;
}
//system("pause");
}