分析:模板题,从后序遍历可知根的位置,在中序遍历中查找根的位置划分左右子树,递归处理。
代码:
#include "bits/stdc++.h"
using namespace std;
string In, Post;
void Solve(int PostB, int PostE, int InB, int InE) {
if (PostB >= PostE) return;
int RootPos = In.find(Post[PostE - 1]);
cout << In[RootPos];
Solve(PostB, PostB + RootPos - InB, InB, RootPos);
Solve(PostB + RootPos - InB, PostE - 1, RootPos + 1, InE);
}
int main() {
cin >> In >> Post;
int size = In.size();
Solve(0, size, 0, size);
return 0;
}