
正文
T1013 求先序排列 codevs
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
http://codevs.cn/problem/1013/
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
题解
查看运行结果
题目描述 Description
给出一棵二叉树的中序与后序排列。求出它的先序排列。(约定树结点用不同的大写字母表示,长度<=8)。
输入描述 Input Description
两个字符串,分别是中序和后序(每行一个)
输出描述 Output Description
一个字符串,先序
样例输入 Sample Input
BADC
BDCA
样例输出 Sample Output
ABCD
数据范围及提示 Data Size & Hint
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>using namespace std;string zx,hx;void get_hx(int l1,int r1,int l2,int r2)
{
int m=zx.find(hx[r2]);
cout<<hx[r2];
if(m>l1)
get_hx(l1,m-,l2,l2+m--l1);
if(m<r1)
get_hx(m+,r1,l2+m-l1,r2-);}int main()
{
cin>>zx>>hx; int r1=zx.length()-;
int r2=hx.length()-;
get_hx(,r1,,r2); return ;
}






