nyoj221_Tree_subsequent_traversal
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 creations:D / \ / \ B E / \ \ / \ \ A C G / / FTo 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 <iostream>
#include <cstdio>
#include <cstring> using namespace std; char pre[];
char in[]; void subsquent(int s1,int s2,int n){
if(n==){
printf("%c",pre[s1]);
}
if(n<=){
return ;
}
int i;
for(i=;pre[s1]!=in[s2+i];i++);
subsquent(s1+,s2,i);
subsquent(s1+i+,s2+i+,n-i-);
printf("%c",pre[s1]);
} int main()
{
while(scanf("%s %s",pre+,in+)!=EOF){
int n=strlen(pre+);
subsquent(,,n);
printf("\n");
}
return ;
}
nyoj221_Tree_subsequent_traversal的更多相关文章
随机推荐
- 密码学初级教程(一)基本概念及DES加密算法
密码技术在网络通信中广泛使用,本节是初步接触密码学技术的笔记. 第1章 加密-解密 破译 明文-密文 密钥 密码算法 对称密码-公钥密码(非对称密码) 单向散列函数-散列值 消息认证码 数字签名 伪随 ...
- [译]git pull
git pull把git fetch和git merge压缩成了一条命令. 用法 git pull <remote> 作用和git fetch <remote> &&a ...
- Tomcat 6 --- 使用Jasper引擎解析JSP
熟悉JAVA web开发的朋友都知道JSP会被转换成java文件(预编译),然后编译成class使用,即按照JSP-->java-->class的过程进行编译. 由于JVM只认识class ...
- MongoDB MapReduce学习笔记
http://cnodejs.org/topic/51a8a9ed555d34c67831fb8b http://garyli.iteye.com/blog/2079158 MapReduce应该算是 ...
- [Storm] java.io.FileNotFoundException: File '../stormconf.ser' does not exist
This bug will kill supervisors Affects Version/s: 0.9.2-incubating, 0.9.3, 0.9.4 Fix Version/s: 0.10 ...
- c语言小知识点
大一时学c语言,总结的一些自己感觉很零碎且容易忘的知识点,不对之处请指正 1.字符串不管中间是否有数值0,结尾一定有数值02.浮点类型的变量存储并不精确3.printf格式串自动右对齐,加负号左对齐4 ...
- sql种类
- web安全及防护
本文将对web方面的安全问题以及相应的防护方法进行一个简单的介绍. SQL注入(SQL Injection) 原理:就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺 ...
- [POJ1328]Radar Installation
[POJ1328]Radar Installation 试题描述 Assume the coasting is an infinite straight line. Land is in one si ...
- Laravel 5.1 文档攻略 —— Eloquent:模型关系
简介 其实大家都知道,数据表之间都是可以关联的,前面讲过了,Eloquent ORM是数据模型操作代替表操作,那么表的关联查询,在Eloquent这里也就是模型间的关联查询,这就是本章的主要内容: E ...