POJ 2255 Tree Recovery

ayit第十周训练题e题 Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her creati... 阅读详情

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:

                                           D                                
                                         /   \
                                        B     E
                                      /   \     \
                                     A     C     G
                                                /
                                              F

To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).

Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!

Input

The input will contain one or more test cases.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.

Output

For each test case, recover Valentine’s binary tree and print one line containing the tree’s postorder traversal (left subtree, right subtree, root).

Sample Input

DBACEGF ABCDEFG
BCAD CBAD

Sample Output

ACBFGED
CDAB

题意描述:

给出一颗数的先序和中序遍历,让你求出后序遍历。

解题思路:

先找到根节点在中序遍历的位置,然后递归构造左子树的后序遍历,之后递归遍历右子树的后序遍历,然后再把根节点加上去,就可以了。

AC代码

#include<string.h>

char s1[50],s2[50],ans[50];

void build(int n,char *s1,char *s2,char *s)
{
	int p;
	if(n<=0)
		return ;
	p=strchr(s2,s1[0])-s2;
	build(p,s1+1,s2,s);
	build(n-p-1,s1+p+1,s2+p+1,s+p);
	s[n-1]=s1[0]; 
}

int main(void)
{
	int n;
	while(~scanf("%s%s",s1,s2)){
		n=strlen(s1);
		build(n,s1,s2,ans);
		ans[n]='\0';
		printf("%s\n",ans);
	}
	return 0;
 } 
poj_2255_Tree Recovery_解题报告 题目出处 题意:输入两组数据,分别是前序遍历序列和中序遍历序列,你需要编写程序通过这两组数据求出该树的后序遍历序列(前序序列 + 中序序列 = 后序序列) 解法:递归 题目分析: 可以先按照用笔和纸的形式去推导出后序序列。推导过程省略,在推导过程中我们会发现规律: 假设 前序序列是 A B E H F C G I  中序序列是 H E B F A C I G (图如下) 每一 阅读详情

相关推荐

手机刷双系统

刷机教程(https://www.geekeyes.cn/forum.php/forum.php?mod=viewthread&amp;tid=492) 解锁教程(http://www.gioov.com/index.php/1386)   解锁教程 cmd下 进入adb目录( 如果环境变量配置了adb 可以直接在cmd 下输入命令) 手机连接电脑,确保usb 调试打开,然后手机连接电脑,...

超宇的博客 1万+

UVA 536 - Tree Recovery ( 二叉树 )

Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one o...

仙女的博客 725

别再只盯着YOLOv8了!手把手带你用PaddleDetection的PP-YOLOE+搞定你的第一个检测项目

本文介绍了PP-YOLOE+在目标检测领域的优势与应用实践,对比YOLOv8展示其在精度、速度和部署便利性上的突破。通过详细的代码示例和实战教程,帮助开发者快速上手PaddleDetection工具链,实现产业级目标检测项目,提升开发效率。

weixin_42701915的博客 185

ACM--二叉树-已知前中序求后序--POJ-2255

北大OJ地址:点击打开链接 Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13336 Accepted: 8322 Description Little Valentine liked playing with bin

戎码人生 1859

全面讲解电子齿轮比与脉冲数计算

什么是编码器分辨率。

viixin的博客 1万+

QGIS v3.28 高效加载OSM在线地图的三种实用方法

本文详细介绍了在QGIS v3.28中高效加载OSM在线地图的三种实用方法:通过XYZ Tiles直接连接、使用QuickMapServices插件以及配置WMS/WTMS服务。针对每种方法提供了具体操作步骤、常见问题解决方案和性能优化建议,帮助用户根据需求选择最适合的方案,提升GIS工作效率。

weixin_29233333的博客 434

二叉树重建

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her creati

renfei_renfei的博客 330

期货分仓系统CTP协议/国内期货软件源码

期货分仓系统与CTP协议以及国内期货软件源码是期货交易技术领域的几个重要方面。

q2247238820的博客 962

tree(nyoj)

Tree 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital lett

u012689941的专栏 631

大疆RoboMaster嵌入式面试,电机FOC调不出来当场走人

摘要:大疆RoboMaster嵌入式工程师面试以实战能力著称,重点考察FreeRTOS任务调度、FOC电机控制和CAN总线三大核心技能。FreeRTOS部分涉及优先级调度、栈溢出排查及信号量与互斥锁的区别;FOC控制要求现场编写电流环代码,并解释Clarke/Park变换原理;CAN总线则关注仲裁机制、CANopen协议应用及错误帧排查方法。面试强调工程实践,需具备STM32开发经验,建议搭建实际环境进行练习。该岗位被誉为"硬件版修罗场",充分体现大疆对实际开发能力的严格要求。

郑伟强dev的专栏 150

汽车零部件产线实战:C#上位机+YOLO实现螺栓装配到位视觉检测

本项目基于C#上位机+YOLOv8实现汽车零部件产线螺栓装配到位视觉检测,全程贴合产线实际场景,从硬件选型、安装调试,到模型训练、上位机开发,再到项目联调、优化扩展,均提供手把手实操指导,代码可直接复制使用,规避了工业视觉产线检测的高频踩坑点。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1331

POJ 2255 Tree Recovery

POJ 2255 Tree Recovery 模板题 #include<iostream> #include<stdio.h> #include<string.h> using namespace std; #define maxn 50 char str1[maxn],str2[maxn]...

WF830830的博客 206

Unity 面试篇|(三)设计模式篇 【全面总结 | 持续更新 | 建议收藏】

设计模式是一套被反复使用、多数人知晓、经过分类编目、代码设计经验的总结。设计模式与方法或库的使用方式不同, 你很难直接在自己的程序中套用某个设计模式。模式并不是一段特定的代码, 而是解决特定问题的一般性概念。你可以根据模式来实现符合自己程序实际所需的解决方案。常常会混淆模式和算法, 因为两者在概念上都是已知特定问题的典型解决方案。但算法总是明确定义达成特定目标所需的一系列步骤, 而模式则是对解决方案的更高层次描述。同一模式在两个不同程序中的实现代码可能会不一样。单例模式。

游戏开发小Y的博客 12万+

POJ 刷题系列:2255. Tree Recovery

POJ 刷题系列:2255. Tree Recovery传送门:POJ 2255. Tree Recovery题意: 给出先序和中序,求后序。 思路: 依旧很水,给定先序易知根结点,从中序中找到根结点就能确定左孩子们和右孩子们,递归建树。最后后序遍历一遍即可。代码如下:import java.io.BufferedReader; import java.io.File; import java

Demon-初来驾到 504

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

空调选购指南:从能效比到舒适性,揭秘真正省电的三大核心

在选购家电时,能效比和变频技术是衡量产品性能与节能效果的基础指标。能效比反映了设备将电能转化为制冷/制热能力的效率,而变频技术通过调节压缩机转速实现精准控温,避免了传统定频空调频繁启停带来的能耗峰值。这些技术的核心价值在于显著降低长期使用成本,同时提升使用舒适度。在空调等大家电的应用场景中,硬件配置、智能控制算法与用户使用习惯共同构成了节能系统工程。本文以具体产品为例,深入剖析了全直流变频和电子膨胀阀等关键硬件如何协同工作,以及智能算法如何优化运行逻辑,从而实现真正的“超省电”体验。

weixin_33841503的博客 323

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

POJ-2255 Tree Recovery

POJ-2255 Tree Recovery 题目链接:POJ-2255 题目大意:给定一棵树的前序遍历和中序遍历,让你输出这颗树的后序遍历 解题思路:二分法的思想???? 代码块: #include<iostream> #include<string> using namespace std; string pre, mid; int n = -1; void s...

鱼头的水题之路 223

poj 2255 Tree Recovery

二叉树,给前序中序求后序

繁华过后你还有我 950

poj2255 Tree Recovery

Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11955   Accepted: 7505 Description Little Valentine liked playing with binary trees very much

Jinbao 652

POJ2255-Tree Recovery

全解题报告索引目录 -> 【北大ACM – POJ试题分类】 转载请注明出处:http://exp-blog.com ------------------------------------------------------------------------- 提示:二叉树遍历而已。。。给出前序和中序,求后序 /* Author: Exp...

ζёСяêτ - 小優YoU 6350

Tree Recovery poj 2255 c++

Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11421   Accepted: 7167 Description Little Valentine liked playing with binary trees very much. He

u012938194的专栏 657

POJ2255 Tree Recovery 二叉树遍历

Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12523   Accepted: 7836 Description Little Valentine liked playing with binary trees very much.

Jonariguez 吾本优秀 1239

poj2255Tree Recovery

数据结构应该有讲,前序遍历和中序遍历 或者是中序遍历和后序遍历可以恢复整个二叉树,然后在建立利用另一种遍历 14272030 h123120 2255 Accepted 688K 0MS G++ 1278B 2015-06-06 17:18:47 #include #include #include #include #include #inc

h123120的专栏 490

Tree Recovery POJ - 2255

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.  This is an example of one of her creati...

Qtt110的博客 330
上一篇: HDU-2546-饭卡(dp)
下一篇: POJ 1700 Crossing River
鳳九~
博客等级 码龄8年 1384粉丝 215原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值