Tree Recovery(前序中序求后序)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 14640 | Accepted: 9091 |
Description
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
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
Sample Input
DBACEGF ABCDEFG
BCAD CBAD
Sample Output
ACBFGED
CDAB
已知二叉树的前序后序遍历求后序遍历。
代码:
注意in的范围的变化和pre一样
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef struct Tree{
Tree *l, *r;
char value;
Tree(){
l = NULL;
r = NULL;
}
}Tree;
int find(char c, char *in){
for(int i = ; in[i]; i++){
if(c == in[i]){
return i;
}
}
}
void make(int n, char *pre, char *in, Tree *&t){
if(n <= )return;
int k = find(pre[], in);
t = new Tree(); t->value = pre[];
make(k, pre + , in, t->l);
make(n - - k, pre + k + , in + k + , t->r);
}
void postVisit(Tree *tree){ if(tree->l)
postVisit(tree->l);
if(tree->r)
postVisit(tree->r);
printf("%c", tree->value);
}
int main(){
char pre[],in[];
while(~scanf("%s%s", pre, in)){
Tree t;
make(strlen(pre), pre, in, t.l);
postVisit(t.l);
puts("");
}
return ;
}
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef struct Tree{
Tree *l, *r;
char value;
Tree(){
l = NULL;
r = NULL;
}
}Tree;
int find(char c, char *in){
for(int i = ; in[i]; i++){
if(c == in[i]){
return i;
}
}
}
Tree* make(int n, char *pre, char *in){
if(n <= )return NULL;
int k = find(pre[], in);
Tree *t = new Tree(); t->value = pre[];
t->l = make(k, pre + , in);
t->r = make(n - - k, pre + k + , in + k + );
return t;
}
void postVisit(Tree *tree){ if(tree->l)
postVisit(tree->l);
if(tree->r)
postVisit(tree->r);
printf("%c", tree->value);
}
int main(){
char pre[],in[];
while(~scanf("%s%s", pre, in)){
Tree *t;
t = make(strlen(pre), pre, in);
postVisit(t);
puts("");
}
return ;
}
Tree Recovery(前序中序求后序)的更多相关文章
- HDU 1710 二叉树遍历,输入前、中序求后序
1.HDU 1710 Binary Tree Traversals 2.链接:http://acm.hust.edu.cn/vjudge/problem/33792 3.总结:记录下根结点,再拆分 ...
- PAT (Advanced Level) 1136~1139:1136模拟 1137模拟 1138 前序中序求后序 1139模拟
1136 A Delayed Palindrome(20 分) 题意:给定字符串A,判断A是否是回文串.若不是,则将A反转得到B,A和B相加得C,若C是回文串,则A被称为a delayed palin ...
- python实现根据前序与中序求后序
我就不板门弄斧了求后序 class Tree(): def __init__(self,x): self.value=x self.left=None self.right=None class So ...
- UVa 二叉树重建(先序+中序求后序)
题意是给出先序和中序,求出后序. 先序遍历先访问根结点,通过根结点可以在中序中把序列分为左子树部分和右子树部分,我建了一个栈,因为后序遍历最后访问根结点,所以把每次访问的根结点放入栈中.因为后序遍历先 ...
- c++树,知道前序和中序求后序遍历
经常有面试题就是知道一棵树的前序遍历和中序遍历让你写出后序遍历,这个慢慢画是能画出来的,但是要很快的弄出来还是要懂原理. 首先说一下三种遍历:所谓的前序后序和中序都是遍历时遍历根节点的顺序.子树的话依 ...
- 【美国血统 American Heritage 题解】已知前序中序 求后序
题目: 题目名称:美国血统 American Heritage 题目来源:美国血统 American Heritage ## 题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的 ...
- ACM题目————已知前序和中序求后序
#include <iostream> #include <cstring> #include <cstdio> using namespace std; ], z ...
- 二叉排序树的构造 && 二叉树的先序、中序、后序遍历 && 树的括号表示规则
二叉排序树的中序遍历就是按照关键字的从小到大顺序输出(先序和后序可没有这个顺序) 一.以序列 6 8 5 7 9 3构建二叉排序树: 二叉排序树就是中序遍历之后是有序的: 构造二叉排序树步骤如下: 插 ...
- 二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)
将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: typedef struct TreeNode{ int data; struct TreeNode *le ...
随机推荐
- Class类文件结构
平台无关性 Java是与平台无关的语言,这得益于Java源代码编译后生成的存储字节码的文件,即Class文件,以及Java虚拟机的实现.不仅使用Java编译器可以把Java代码编译成存储字节码的Cla ...
- OCiOS开发:音频播放器 AVAudioPlayer
简单介绍 AVAudioPlayer音频播放器可以提供简单的音频播放功能.其头文件包括在AVFoudation.framework中. AVAudioPlayer未提供可视化界面,须要通过其提供的播放 ...
- iptables nat 外网nat到内网在只限制外网访问的单一ip地址
166 /etc/init.d/iptables start 167 iptables -I INPUT -s 192.168.10.0/24 -p tcp -j ACCEPT 168 /etc/in ...
- Java设计模式(1)工厂模式(Factory模式)
工厂模式定义:提供创建对象的接口. 为何使用工厂模式 工厂模式是我们最常用的模式了,著名的Jive论坛,就大量使用了工厂模式,工厂模式在Java程序系统可以说是随处可见. 为什么工厂模式是如此常用?因 ...
- [Intellij] 软件设置和常用快捷键
理解Project 和 Module 一个 Project 是由一个或多个 Module 组成,模块之间尽量是处在同一个项目业务的的情况下,彼此之间互相依赖关联.如: Project : spring ...
- [技术选型] dubbo
分布式服务架构 - 阿里开源项目 简介 Dubbo架构设计详解 Dubbo与Zookeeper.SpringMVC整合和使用(负载均衡.容错)
- [hadoop读书笔记] 第十五章 sqoop1.4.6小实验 - 数据在mysq和hdfs之间的相互转换
P573 从mysql导入数据到hdfs 第一步:在mysql中创建待导入的数据 1.创建数据库并允许所有用户访问该数据库 mysql -h 192.168.200.250 -u root -p CR ...
- QTcpSocket的连续发送数据和连续接收数据
关于这个问题折腾了我好久,以前做些小练习的时候,用QTcpSocket的write()一数据,然后接收方只要emit一个readyread()信号然后就用QTcpSocket的read()去读.本以为 ...
- 利用函数或映射进行数据转换 (map)
先来看个数据 df = DataFrame({"food":["bacon", "pulled pork", "bacon&quo ...
- 【转】【Python】Python中的__init__.py与模块导入(from import 找不到模块的问题)
python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文 件,在另一个文件中需要import时,将事先写好的.py文件拷贝 到当前目录,或者是在sys.path中增加事先写好的 ...