题意:
给出一个前序遍历序列和中序遍历序列,输出后序遍历序列
跟之前的给后序和中序遍历序列一样
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 30;
int l[maxn],r[maxn];
string fo_order,in_order;
void build(int l1,int r1,int l2,int r2)
{
if(l1 > r1){
return ;
}
char root = fo_order[l2];
int k = l1;
while(in_order[k] != root){
k++;
}
int cnt = k - l1;
build(l1,k - 1,l2 + 1,l2 + cnt);
build(k + 1,r1,l2 + cnt + 1,r2);
cout<<root;
}
int main()
{
while(cin>>fo_order>>in_order){
int strl = fo_order.size();
build(0,strl - 1,0,strl - 1);
cout<<endl;
}
return 0;
}