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. 源码编译安装 screen

    本文转自:http://blog.163.com/oracle_wwf/blog/static/213030127201211191481101/ [root@web1 soft]# wget ftp ...

  2. php 快速排序法

    function quicksort(array $arr = array()){ $len = count($arr); if ($len > 1) { $key = $arr[0]; $l_ ...

  3. ThinkPHP3.2对接开发支付宝即时到帐接口

    ThinkPHP3.2对接开发支付宝即时到帐接口 在做一些商城.自动发卡网站.会员积分充值.金币充值等等这类网站都时候,我们极大可能需要使用到第三方都支付接口.不管是财付通.支付宝.银联.贝宝.易宝这 ...

  4. PHP支付宝接口RSA验证

    这两天一直困扰的PHP RSA签名验证问题终于解决了,由于之前RSA接触的不多,再加上官方至今还未有PHP的SDK可供参考,因此走了一些弯路,写在这里和大家分享.    虽然支付宝官方还未提供相关SD ...

  5. github 上传至远程的过程

    参考网址:http://luolei.org/dotfiles-tutorial/ http://www.ruanyifeng.com/blog/2014/06/git_remote.html     ...

  6. js字符串与16进制互相转换

    // \x65\x76\x61\x6c是否启用\x加密 <script type="text/javascript"> function JavaDe() { var ...

  7. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

  8. Android学习笔记(六)——活动的启动模式

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 活动的启动模式共有四种: standard.singleTop.singleTask 和 singleInst ...

  9. 剑指Offer 二维数组中的查找

    题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路法一: * 矩阵是 ...

  10. 分享一个快速设置背景的js 自动获取背景图的长宽

    我来分享一个快速设置背景的js (需要jq支持!) 快速切图铺页面用---就是不需要手动输入背景图的长宽 自动获取背景图的长宽 : <div class="wrap"> ...