1013 求先序排列

题目描述 Description

给出一棵二叉树的中序与后序排列。求出它的先序排列。(约定树结点用不同的大写字母表示,长度<=8)。

输入描述 Input Description

两个字符串,分别是中序和后序(每行一个)

输出描述 Output Description

一个字符串,先序

样例输入 Sample Input

BADC

BDCA

样例输出 Sample Output

ABCD

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
string s1,s2;
void calc(int l1,int r1,int l2,int r2)
{
	int m=s2.find(s1[r2]);
	cout<<s1[r2];
	if(m>l1) calc(l1,m-1,l2,l2+m-l1-1);
	if(m<r1) calc(m+1,r1,l2+m-l1,r2-1);
}
int main()
{
	cin>>s2>>s1;
	int x=s1.size(),y=s2.size();
	calc(0,y-1,0,x-1);
	cout<<endl;
	return 0;
}
	

方法二:
#include<iostream>

#include<cstring>

#include<cstdio>

using namespace std;

void abc(int x,int x1,int y,int y1);

string s1,s2;

int main()

{

cin>>s1>>s2;

abc(0,s1.length()-1,0,s2.length()-1);

return 0;

}

void abc(int x1,int x,int y1,int y)

{



int m=s1.find(s2[y]);

cout<<s2[y];

if(m>x1)

abc(x1,m-1,y1,y1+m-x1-1);

if(m<x)

abc(m+1,x,y1+m-x1,y-1);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值