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;
}
Mathtype公式排版避坑指南:固定行距20磅下如何完美显示上下标(实测有效) 本文针对学术论文在固定行距20磅要求下,Mathtype公式上下标显示不全或被裁剪的问题,提供了实测有效的解决方案。文章深入剖析了固定行距与公式排版的冲突原理,并详细指导用户通过调整Mathtype的“定义间距”参数(如下标深度、上标高度),以及利用“格式化公式”功能进行批量处理,确保公式在严格遵守期刊格式要求的同时清晰完整显示。 阅读详情

相关推荐

Easymesh之拓扑模块(二)

介绍 easymesh 拓扑消息:Topo discovery、Topo notification、Topo query、Topo response

qq_53619687的博客 2174

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

2023-2024深度学习框架之争——选pytorch还是tensorflow?

深度学习是人工智能领域的一个重要分支,它利用多层神经网络来模拟人类的学习和推理能力,解决各种复杂的问题,如图像识别、自然语言处理、语音识别、推荐系统等。深度学习框架是一种软件工具,它提供了构建、训练、测试和部署深度学习模型的便利,使得开发者和研究者可以更高效地进行深度学习的开发和应用。目前,市场上有许多不同的深度学习框架,如PyTorch、TensorFlow、Keras、MXNet、Caffe2等,它们各有优劣,适用于不同的场景和需求。

flchen_nchu的博客 6769

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

西门子840D新建项目

1、需要一个32位的WIN7或者XP系统,并且已经在系统中安装完成西门子840Dsl开发包和VS2008。(我采用的是XP系统虚拟机) 2、在开始->所有程序找到如下位置 3、在这个位置打开Visual Studio 4、文件->新建->项目 名称和位置都可以随意更改,点击确定。 5、在左侧选择第二个选项进入下面的界面 点击“Add Screen”按钮 可以修改“class name”然后点击“OK”返回上一个界面,再点击“Add Form” 可以修...

qq_36987047的博客 1872

多周期路径及set_multicycle_path详解

默认情况下,每条路径都被定义为单周期,即源触发器在时钟的任一边沿启动(launch)的数据都应该由目的触发器在时钟的下一上升沿捕获(capture)。 有的设计需要在数据被捕获前提供一些额外的周期,这类路径被称为多周期路径。 一、set_multicycle_path命令 命令格式如下: set_multicycle_path .........

Jackiezhang1993的博客 4万+

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

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

crazy__C的专栏 615

V4L2教程(1):Ubuntu V4L2开发环境搭建与设备探索

欢迎来到V4L2学习指南的第一篇!本文将带你从零开始,在Ubuntu系统上搭建完整的V4L2视频采集开发环境。无论你是想开发视频监控、视频会议还是机器视觉应用,掌握V4L2都是Linux视频开发的必经之路。 **本节学习目标**: - 理解V4L2的基本概念和架构 - 搭建Ubuntu下的V4L2开发环境 - 掌握v4l2-ctl等调试工具的使用 - 学会检测和配置USB摄像头设备

琅嬛福地 429

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软件库 992

基于STM32、HAL库的W25Q01JVZEIQ TR FLASH存储器驱动应用程序设计

1Gb (128MB),分为512个可擦除块(block),每个块256KB。:块擦除约30-50ms,芯片擦除约30s。:标准SPI (支持1/2/4线模式)// 初始化CS引脚为高电平。// 擦除扇区(4KB)// 初始化Flash。/* 状态寄存器位 *//* 指令定义 *//* 超时定义 *//* 函数声明 */

corlin6688的博客 274

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

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

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

NTscan扫描工具

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

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 2510

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

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

柚的个人技术博客 1709

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的专栏 811

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

你身后的人 225

nyoj 221 Tree

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

哇塞 320

NYOJ221 Tree

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

kay_zhyu的专栏 1152

nyoj 221-Tree

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

shofe的学习随记 651

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

狂飙的蜗牛 的专栏 1043

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

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

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址