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时间操作:获取当天剩余时间
mport org.apache.commons.lang.time.DateUtils; import org.joda.time.DateTime; import org.joda.time.Du ...
- SQL自定义函数
1,自定义函数--返回单一值 CREATE FUNCTION [dbo].[Round2] ( -- Add the parameters for the function here @p1 sql_ ...
- WebForm发送邮件
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Ne ...
- IOC容器:Unity
一.什么是IOC 学习IOC之前先来了解一个依赖导致原则(DIP),依赖导致原则是IOC的核心原理. 依赖导致:即上层模块不应该依赖于低层模块,二者应该通过抽象来依赖.依赖于抽象,而不是依赖于细节. ...
- CSS弹出背景半透明窗口
<script type="text/javascript" src="<ww:url value='/js/jquery-1.8.3.min.js'/> ...
- mysql 删除
DROP删表,表结构将删了,当然数据也不存在了 TRUNCATE和DELETE删数据,表结构还在 DELETE可以带条件删除,TRUNCATE是全部删除 DELETE删除会写日志,TRUNCATE不写 ...
- C语言实现Linux下删除非空目录
#include <sys/stat.h> #include <dirent.h> #include <fcntl.h> /** * 递归删除目录(删除该目录以及该 ...
- win10 + linux 制作双系统教程(我本身是win10系统)
1.制作启动U盘 准备工作: .linux镜像 .硬盘空余空间>8G,越大越好 .制作启动U盘的软件 .最好3.0U盘一个>4G 下载启动软件的工具(UItraIOS制作的U盘启动盘无法安 ...
- 技能UP:SAP OBYC自动记账的实例说明(含value String应用说明)
一. 自动过账原理 在MM模块的许多操作都能实现在FI模块自动过账,如PO收货.发票验证(LIV).工单发料.向生产车间发料等等.不用说,一定需要在IMG中进行配置才可以实现自动处理.但SAP实现的这 ...
- Linux C语言连接 sqlserver数据库
记录一下Linux下使用C语言连接sqlserver的方法. 连接前需要安装freetds. 参考: http://www.cnblogs.com/helloworldtoyou/p/6910075. ...