Tree

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述
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的更多相关文章

随机推荐

  1. .NET之委托

    有些.NET中的高级特性,比如:委托! 有一种怎么也搞不懂的赶脚... 博客读了好几篇,代码也动手写了,书中的一些介绍也看了, 各种搜索关于委托的,至今还处于"会用"的阶段. 该怎 ...

  2. 2015baidu复赛 矩形面积(包凸 && ps:附quickhull模板)

    矩形面积 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. 【PHP面向对象(OOP)编程入门教程】3.什么是面向对象编程呢?

    就不说他的概念,如果你想建立一个电脑教室,首先要有一个房间, 房间里面要有N台电脑,有N个桌子, N个椅子, 白板, 投影机等等,这些是什么,刚才咱们说了, 这就是对象,能看到的一个个的实体,可以说这 ...

  4. C#之XMAL与WPF

    XAML的简单说明 XAML是用于实例化.NET对象的标记语言,主要用于构建WPF的用户界面 XAML中的每一个元素都映射为.NET类的一个实例,例如<Button>映射为WPF的Butt ...

  5. qt 的简介与使用

    1.图形类的构造,都是通过类的构造函数以及界面初始化这两部分 2.在qtcreator的界面编辑器通过“提升类”的方法,要确定头文件的路径是否正确 3.点击窗口关闭时,销毁窗口内容时,设置属性-> ...

  6. springmvc之图片上传

    1.接收到的是图片的流时 //上传头像 @RequestMapping(value = "/uploadHeadSculpture", method = RequestMethod ...

  7. 在 Ubuntu 16.04 上安装 LEMP 环境之图文向导

    导读 LEMP 是个缩写,代表一组软件包(注解 ① L:Linux OS,E:Nginx 网络服务器,M:MySQL/MariaDB 数据库和 P:PHP 服务端动态编程语言),它被用来搭建动态的网络 ...

  8. MySQL 同主机不同数据库之间的复制

    MySQL同主机不同数据库的复制命令:注意运行在Terminal中,不运行在MySQL命令行中. mysqldump Portal_DEV -u root -ppassword1 --add-drop ...

  9. OpenCV图像的二值化

    图像的二值化: 与边缘检测相比,轮廓检测有时能更好的反映图像的内容.而要对图像进行轮廓检测,则必须要先对图像进行二值化,图像的二值化就是将图像上的像素点的灰度值设置为0或255,这样将使整个图像呈现出 ...

  10. 单臂路由与三层交换机实现VLAN通信

    不同VLAN之间相互通信的两种方式 (单臂路由.三层交换) 试验环境:东郊二楼第三机房 试验设备:Catalyst 2950-24(SW3)                   Cisco 2611( ...