luogu1030 求先序序列(NOIP2001普及组第3题)

luogu1030 求先序序列(NOIP2001普及组第3题)

时空限制    1000ms/128MB

题目描述

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

输入输出格式

输入格式:

2行,均为大写字母组成的字符串,表示一棵二叉树的中序与后序排列。

输出格式:

1行,表示一棵二叉树的先序。

输入输出样例

输入样例#1:

BADC
BDCA

输出样例#1:

ABCD

 

 

代码

#include<iostream>
#include<string>
using namespace std;
struct node{
	char data;
	node *lch,*rch;
};

void create(node *&root,string s1,string s2){
	if (s1=="") { root=NULL; return; }
	root = new node;
	int len=s1.size();
	root->data = s2[len-1];
	int pos=s1.find(s2[len-1]);
	create(root->lch,s1.substr(0,pos),s2.substr(0,pos));
	create(root->rch,s1.substr(pos+1,len-pos-1),s2.substr(pos,len-pos-1));
}

void firstord(node *root){
	if (root){
		cout<<root->data;
		firstord(root->lch);
		firstord(root->rch);
	}
}

int main(){
	string s1,s2;
	cin>>s1>>s2;
	node *root(NULL);
	create(root,s1,s2);
	firstord(root);
	cout<<endl;
	return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值