UVa 536:Tree Recovery(水题)

本文介绍了一种从先序遍历和中序遍历来重建二叉树,并输出其后序遍历序列的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值