您的位置:首页 > 其它

UVa 536:Tree Recovery(水题)

2015-09-19 09:22 316 查看
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=839&page=show_problem&problem=477

题意:输入一颗二叉树的先序遍历和中序遍历序列,输出后序遍历序列。(本段摘自《算法竞赛入门经典(第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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: