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 ...
随机推荐
- Extjs4.2 Grid搜索Ext.ux.grid.feature.Searching的使用
背景 Extjs4.2 默认提供的Search搜索,功能还是非常强大的,只是对于国内的用户来说,还是不习惯在每列里面单击好几下再筛选,于是相当当初2.2里面的搜索,更加的实用点,于是在4.2里面实现. ...
- UDP丢包原因
一.主要丢包原因 1.接收端处理时间过长导致丢包:调用recv方法接收端收到数据后,处理数据花了一些时间,处理完后再次调用recv方法,在这二次调用间隔里,发过来的包可能丢失.对于这种情况可以修改接收 ...
- 无法加载 Parallels 驱动器
解决 最新版 mac 系统 无法使用未签名第三驱动 10.12.多 我的情况是 10.11.4 Beta (15E27e) 使用绿联usb网卡不正常. 下面的命令为检测驱动是否装载的一些命令.sudo ...
- vnc viewer on Ubuntu
我使用的是putty和SSL/SSH Vnc Viewer.因为刚入坑,也希望小白们能少走弯路,所以本贴写得比较细. 先说说vnc server和 vnc viewer的区别:server用于服务器, ...
- Selenium常用操作汇总二——如何得到弹出窗口
在selenium 1.X里面得到弹出窗口是一件比较麻烦的事,特别是新开窗口没有id.name的时候.当时还整理了处理了几种方法,详见:http://seleniumcn.cn/read.php?ti ...
- tomcat部署时war和war exploded区别
war模式—-将WEB工程以包的形式上传到服务器 war exploded模式—-将WEB工程以当前文件夹的位置关系上传到服务器
- 【转】【Python】Python多进程与多线程
1.1 multiprocessing multiprocessing是多进程模块,多进程提供了任务并发性,能充分利用多核处理器.避免了GIL(全局解释锁)对资源的影响. 有以下常用类: 类 描述 P ...
- Android wifi powersave
使用高通平台的查看power save的功能. 一般是控制WCNSS_qcom_cfg.ini文件的两个参数gEnableBmps,gEnableImps. BMPS: Beacon mode pow ...
- 多媒体开发之rtmp---rtmp client 端的实现
去年我就在流媒体朋友圈认识winlin 老乡,本来想参与srs的开发,可惜今年5月份身体不好,今天想起rtmp 做直播,有翻到老乡的博文如下: http://blog.csdn.net/win_lin ...
- C# js 在页面能执行,放在单独js文件不能执行
我们先来看看MVC中生成的 注意:url.Content 生成的路径 Html.ActionLink 与 Url.Action 1.两者者是根据给定的Controller,Action 生成链接, 但 ...