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 (图如下) 每一 阅读详情

相关推荐

VsCode+JLink+GCC替代Keil开发GD32程序-个人总结

对个人使用vscode+Jlink开发GD32程序做了总结,供需要者参考。

MyCollege1999的博客 2765

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

交叉编译Qt6.5.3(RK3568开发板)

注意,如果使用其它版本的交叉编译器的话,请确保你的交叉编译器能够支持 C++17标准。为确保后续交叉编译没问题,需要确保开发板使用的GLIBC版本不小于2.34版本。到 linaro 网站上下载交叉编译工具,我下载的是 gcc-linaro-11.3.1-2022.06-x86_64_aarch64-linux-gnu.tar.xz。安装 CMake ,最低要求3.16版本,如果要静态编译,需要3.21及以上版本。我使用的3.27版本。:指定为最后交叉编译后的Qt6.5.3库的安装目录。

m0_46309425的博客 5104

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

干扰管理学习日志4-------信道估计方法 LS(最小二乘)、MMSE(最小均方误差)

信道估计,就是从接收数据中将假定的某个信道模型的模型参数估计出来的过程。如果信道是线性的话,那么信道估计就是对系统冲激响应进行估计。需强调的是信道估计是信道对输入信号影响的一种数学表示,而“好”的信道估计则是使得某种估计误差最小化的估计算法。本文主要讲述非盲信道估计中的LS估计和MMSE估计。LS全称为,即估计的信道参数与实际信道参数的差,进行平方求和后得到的误差累积最小。MMSE估计是LS估计的改进,在LS估计的最终结果的基础上乘以加权矩阵,使之与实际信道参数更为接近。

taiyuezyh的博客 1万+

从0到1:提示工程架构师搭建Agentic AI系统的全流程实战手册

Agentic AI(自主智能体)是具备自主决策能力的大模型应用感知(Perceive):获取外部环境信息(比如代码仓库、用户输入、数据库数据);记忆(Memory):存储和检索历史信息(比如过去的审计记录、用户偏好);规划(Plan):将复杂任务拆解为可执行的步骤(比如“代码审计”拆成3步);行动(Act):调用工具或API完成具体操作(比如调用Git克隆仓库、调用Safety查漏洞);反馈(Feedback):根据结果调整策略(比如用户说“报告不够详细”,下次增加逻辑漏洞的分析深度)。

AI Agent Harness Engineering 1042

二叉树重建

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

总结:知识图谱KG+大模型LLM

知识图谱KG+大模型LLM

安静的软件工程师 4211

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

玩转Docker小游戏项目系列: Docker部署红心纸牌网页小游戏

玩转Docker小游戏项目系列: Docker部署红心纸牌网页小游戏

jks212454的博客 421

hwasan / asan详细分析踩内存之一:Use after free

UAF漏洞全称为use after free ,即利用已经被free掉的堆块被再次利用,该漏洞的存在是因为程序编写者在free堆块部分没有将该堆块的fd指针改为0,进而导致该堆块可以被申请回来,3.内存块被free后,其对应的指针没有被设置为 NULL,但是在它下一次使用之前,有代码对这块内存进行了修改,那么当程序再次使用这块内存时,就很有可能会出现奇怪的问题。2.堆块free后,对应指针没有被设置为NULL,即还可以调用会该堆块,当我们申请一个比该堆块小于等于的size时。

1398

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

从被秒封到稳过Cloudflare!Canvas/WebGL/WebRTC多维度浏览器指纹隐身全实战

反爬和反反爬,本质上是一场持续的博弈,没有100%的隐身,只有不断升级的伪装。网上很多教程只给了个基础的噪声代码,却没讲透反爬的检测逻辑,导致你用了还是被封。这套方案的核心,不是简单的API重写,而是站在反爬的角度,模拟真实用户的所有特征,保证所有指纹的一致性、合理性、完整性。目前这套方案我用了半年,跑了上千万次请求,稳过国内国外99%的主流反爬平台,没有出现过一次因为指纹关联导致的批量封禁。大家有什么更狠的反爬场景,或者更好的伪装技巧,欢迎评论区一起交流。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1194

POJ 刷题系列:2255. Tree Recovery

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

Demon-初来驾到 504

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

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

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

weixin_33841503的博客 323

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

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

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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、付费专栏及课程。

余额充值