题意:输入一颗二叉树的先序遍历和中序遍历序列,输出后序遍历序列。(本段摘自《算法竞赛入门经典(第2版)》)
分析:
水题,二叉树的重建。
代码:
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <cctype>
#include <stack>
#include <set>
#include <map>
using namespace std;
const int maxn = 100 + 5, INF = 1e9;
string a, b;
void recovery(string x, string y)
{
if (x.size() == 0)
return;
if (x.size() == 1)
{
cout << x;
return;
}
int pos = y.find(x[0]);
recovery(x.substr(1, pos), y.substr(0, pos));
recovery(x.substr(pos + 1), y.substr(pos + 1));
cout << x[0];
}
int main()
{
while (cin >> a >> b)
{
recovery(a, b);
printf("\n");
}
return 0;
}