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 ...
随机推荐
- Java编程的逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- 非常有用的15个Linux 服务器监控命令
如果你想知道你的服务器正在做干什么,你就需要了解一些基本的命令,一旦你精通了这些命令,那你就是一个 专业的 Linux 系统管理员. 有些 Linux 发行版会提供 GUI 程序来进行系统的监控,例如 ...
- RavenDb学习(十)附件,存储大对象
.读取 Raven.Abstractions.Data.Attachment attachment = documentStore.DatabaseCommands.GetAttachment(&qu ...
- Error:不能将"char*"类型的值分配到"LPSTR"类型的实体 也许 "char*"类型的实参与"LPCWSTR"类型的形参不兼容
http://www.myexception.cn/ruby-rails/1876106.html 选择“XXX项目”->“属性”->“配置属性”->“常规”选项中,把“使用 Uni ...
- PHP 日期 加减 月数,天数,周数,小时,分,秒等等
实就是strtotime 这个内置函数 //PHP 日期 加减 周 date("Y-m-d",strtotime("2013-11-12 +1 week")) ...
- React Native布局
一款好的APP离不了一个漂亮的布局,本文章将向大家分享React Native中的布局方式FlexBox. 在React Native中布局采用的是FleBox(弹性框)进行布局. FlexBox提供 ...
- Entity Framework Code First - Change Tracking
In this post we will be discussing about change tracking feature of Entity Framework Code First. Cha ...
- Java注释规范整理
Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:00000099 EndFragment:00018736 在软件开发的过程中总是强调注释的规范,但 ...
- SAP NUMBER RANGE维护配置object FBN1 Deletion only possible if status is initial
背景: 错误日志: SAP FBN1 Deletion only possible if status is initial 场景: 如果目标机已有NUMBER RANGE 不为0,需要删除配置年为9 ...
- 用ARM实现音乐电子相册
(前段时间在做嵌入式的课程设计,特将学习心得整理如下) 一.开发工具及环境介绍 1.ARM处理器 ARM处理器是一个32位元精简指令集(RISC)处理器架构,其广泛地使用在许多嵌入式系统设计. ARM ...