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 / / 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!
- 输入
- 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的更多相关文章
随机推荐
- php开发总结
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=shared,mys ...
- 自执行的匿名函数!function()
最近有空可以让我静下心来看看各种代码,function与感叹号的频繁出现,让我回想起2个月前我回杭州最后参加团队会议的时候,@西子剑影抛出的一样的问题:如果在function之前加上感叹号 (!) 会 ...
- linux命令行netstat总结
1.所谓的监听就是某个服务程序会一直常驻在内存中,所以该程序启动的Port就会一直存在. 2.在小于1023的端口,都是需要以root身份才能够启动的. 3.大于1024以上的Port主要是作为cli ...
- mongodb_修改器($inc/$set/$unset/$push/$pop/upsert......)
主从复制:http://blog.csdn.net/drifterj/article/details/7833883 对于文档的更新除替换外,针对某个或多个文档只需要部分更新可使用原子的更新修改器,能 ...
- R笔记 map_leaflet googlevis
packages : map leaflet library(leaflet) library(maps) mapStates = map("state", fill = TRUE ...
- XDU 1160 - 科协的数字游戏I
Problem 1160 - 科协的数字游戏I Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 184 ...
- Hadoop 之Hbase命令
一.常用命令:(hbase shell 进入终端) 1.创建表: create 'users','user_id','address','info' 表users,有三个列族user_id,addre ...
- 大数据之nutch
一.nutch简介 nutch是大名鼎鼎的Doug Cutting发起的爬虫项目,nutch孵化了现在大数据处理框架Hadoop.在nutch V 0.8.0 版本之前,Hadoop是nutch的一部 ...
- Ubuntu 14 常用“快捷键”,Ctrl + Alt + F1 进入终端,按 Ctrl + Alt + F7 回到界面
Ubuntu中所谓 Super键,就是 Windows建,一般在键盘的 ctrl 和 alt 2个键之间,一个微软窗口的图标. 1.持续按住 Super键,会弹出“键盘快捷键”大全: 2.修改快捷键路 ...
- Javascript高级程序设计——引用类型
对象在javascript中被称为引用类型的值,而且有一些内置的引用类型可以创建特定的对象: 引用类型与传统面向对象中的程序设计的类相似,但实现不同: Object是一个基础类型,其他所有类型都从Ob ...