NYOJ221Tree

Tree Recovery 哈工大hoj:ACM试题答案。建树后后续遍历出答案 立即下载

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 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! 
输入
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.
输出
For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).
样例输入
DBACEGF ABCDEFG
BCAD CBAD
样例输出
ACBFGED
CDAB
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
typedef struct Node
{
	char data;
	struct Node *Lchild,*Rchild;
}*BiTree,BiTreenode;
void CreateTree(char *pre, char *mid, BiTree &T, int len)
{
	if (len == 0)
	{
		T = NULL;
		return ;
	}
	char ch = pre[0];
	int index = 0;
	while (mid[index] != ch)
		index++;
	T = (BiTree)malloc(sizeof(BiTreenode));
	T->data = ch;
	CreateTree(pre+1, mid, T->Lchild, index);
	CreateTree(pre+index+1, mid+index+1,T->Rchild, len-index-1);
}
void PrintTree(BiTree T)
{
	if (T != NULL)
	{
		PrintTree(T->Lchild);
		PrintTree(T->Rchild);
		printf("%c",T->data);
	}
}
int main(void)
{
	int len;
	char pre[27],mid[27];
	while (~scanf("%s%s",pre,mid))
	{
		len = strlen(pre);
		BiTree T=NULL;
		CreateTree(pre, mid, T, len);
		PrintTree(T);
		printf("\n");
	}
	return 0;
}
国科大学习资料--人工智能原理与算法-2020年期末考试题解析(学长整理) 国科大学习资料–人工智能原理与算法-2020年期末考试题解析 张文生老师主讲 试卷共有6道题,题目不难,希望大家能取得好的成绩。 3.9 传教士和野人问题。三个传教士和三个野人在河的一岸,有一条能载一个人或者两个人的船。请设法使所有人都渡到河的另一岸,要求在任何地方野人数都不能多于传教士的人数。这个问题在AI领域中很有名,是因为它是第一个从分析的观点探讨问题形式化的论文的主题(Amarel,1968)。 a. 请对该问题进行详细形式化,只描述确保该问题求解所必需的特性。画出完整的状态空间图。 b. 应用合适 阅读详情

相关推荐

从零开始STM32cubeide学习

使用的是正点原子的精英版,单片机是STM32F103ZET6,详细讲解完成跑马灯实验

weixin_44077208的博客 2285

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

Chroma向量数据库实战:用Python快速搭建本地知识库(附中文诗词检索案例)

本文详细介绍了如何使用Chroma向量数据库和Python快速搭建本地知识库,并附有中文诗词检索的实战案例。通过解析核心概念、数据准备、向量化及智能检索实现,展示了Chroma在处理中文语义搜索时的轻量级与易用性,为开发者构建AI应用原型提供了实用指南。

weixin_29221753的博客 480

POJ 2255 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 of...

weixin_34059951的博客 526

Python爬虫实战:5分钟抓取东方财富股票数据并保存为CSV

在投资分析与量化研究的日常工作中,获取结构化、可用的金融数据往往是第一步。对于个人投资者和量化入门者而言,如何高效获取A股全市场行情,常常面临网页动态加载、反爬机制与数据整理繁琐等挑战。本文从数据采集的基础概念出发,介绍如何利用Python这一通用编程语言,通过requests库请求财经网站公开的HTTP接口,获取返回的JSON数据,再借助pandas进行字段映射、数据清洗与表格化处理。这种方式相比传统页面解析具有响应快、结构稳定、易于批量操作的显著优势,尤其适合构建个人股票数据池、验证选股策略或进行市场情

weixin_33146151的博客 251

灰度重心法实战:从原理到MATLAB代码实现激光条纹中心线提取

本文详细介绍了激光条纹中心线提取的核心方法——灰度重心法。通过物理类比阐释其原理,并提供从图像预处理到MATLAB代码实现的完整实战教程。文章重点解析了算法流程、关键参数调整及常见问题避坑指南,帮助读者快速掌握这一机器视觉与三维测量中的基础且高效的技术。

cake8的博客 462

zoj cut the tree(树形dp,小细节真的很多)

这道题做的我很是头痛,好不容易思路有了结果调了好几个小时也没调对,太弱,最后是找了份题解“对照”人家代码改的才过,索性把他的思路也直接粘过来了。  一看便知是树形dp,dp[i][j]-表示以节点 i 为根的树砍 j 次得到的最小权值,不过这题难就难在dp[u][0]有双重含义。  1.自己的初始权值。(便于转移)  2.以u为根的树砍 0 次得到的最小权值。  这样直接采取 dp[u][

crazy__C的专栏 615

别再到处找资源了!WinCC 7.5 SP2官方下载与Windows 10保姆级安装避坑指南

本文提供了WinCC 7.5 SP2在Windows 10上的官方下载与安装全流程指南,重点解决了资源获取难、系统兼容性问题及分卷压缩处理等常见痛点。通过详细的步骤和实用技巧,帮助用户顺利完成安装并优化配置,适用于工控领域的学习者和从业者。

weixin_32924297的博客 496

NYOJ-题目221 Tree

NYOJ-221 Tree view source 01.    02.#include "iostream" 03.#include "cstdio" 04.#include "cstdlib" 05.#include "cstring" 06.   07.using namespace  std; 08.  

Hanks软件库 987

压缩包密码破解工具ZIP Cracker2.0.1.2 正式版

一款多功能文件密码破解工具,支持ZIP、RAR、Office等主流加密格式,提供CPU/GPU双模式破解。该软件采用黑客风格界面,具备环境自检修复功能,支持实时监控硬件使用率。内置hashcat等专业破解工具,通过拖拽文件即可快速破解,GPU模式速度可达CPU的10-100倍。最新版本优化了中文路径支持,并提升了启动速度。

bfq05234214的博客 483

SAP MD09 事务码中的物料需求追溯详解

物料需求追溯是指在物料需求计划中,追踪特定物料的需求来源和满足方式的过程。它帮助企业确定每个需求(如销售订单、独立需求、预留等)是由哪些供应元素(如库存、生产订单、采购订单等)来满足的。通过这种追溯,企业可以了解需求和供应之间的关系,从而优化生产和采购计划,减少库存积压,提升供应链效率。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1197

NYOJ 题目221 Tree

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

小寒 475

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

NYOJ 221 Tree (二叉树遍历)

题目221 题目信息 运行结果 本题排行 讨论区 Tree 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述Little Valentine liked playing with binary trees very much. Her favorite game was constructing ran

MADE_Y 2508

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

NYOJ 221 Tree

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

柚的个人技术博客 1708

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

nyoj221 Tree

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 cre

love超1314的专栏 808

nyoj 221 Tree

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 letters in the nodes. ...

你身后的人 223

nyoj 221 Tree

已知二叉树的先序,中序排列,求它的后序排列

哇塞 320

NYOJ221 Tree

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=221 题目分析: 还是那样,作为一个二逼青年,我首先想到的还是定义一个NODE的结构体,然后建立树,然后.......可是,你有没有想过字符串的感受啊~~也许,你经常忽视的人可能会给你最好的东西。废话不多说,尽管是二逼青年的想法,也要说出来嘛~都有表达想法的权利是不? PS:咳咳~~

kay_zhyu的专栏 1150

nyoj 221-Tree

http://acm.nyist.net/JudgeOnline/problem.php?pid=221 1、前序遍历的第一个节点是根节点。 2、在中序遍历中找到这个根节点,然后把中序遍历的字符串分成了左串和右串,也就是该根节点的左子树和右子树。 3、递归下去就可以了,递归的出口就是root后面的节点个数小于等于0. #include #include #include using n

shofe的学习随记 649

nyoj-221-Tree

#include #include void build_tree(int n,char *a,char *b,char *c){         int tt=0;  char *p=b;     if(n         return ;     while(1){         if(a[0]==*p){//找到中序表示中根的位置             break;

u011514451的专栏 433

nyoj-221 Tree

大概意思就是用的递归,先向左子树递归,然后向右子树递归。。。 #include #include char str[100],zf[100]; void deal(int num_x,int num_y,int num_a,int num_b){ int i,j,k,t,n,m; k=0; for(i=num_a;i<=num_b;i++){ if(zf[i]==str[num_x])

wojiaohuangyu的专栏 502

NYOJ-221 Tree(前序中序推后序)

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

狂飙的蜗牛 的专栏 1040

NYOJ 221 Tree (二叉树遍历)

Tree 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 cr...

我想要西瓜 324
上一篇: NYON28大数阶乘
下一篇: 最小生成树
java_oracle_c
博客等级 码龄13年 2粉丝 29原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值