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!

Input
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.

Output
For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).

Sample Input
DBACEGF ABCDEFG
BCAD CBAD

Sample Output
ACBFGED
CDAB

题意

已知先序和中序,求后序

题解

这题是处理字符,跟处理数字类似,改一点点

代码

 #include<bits/stdc++.h>
using namespace std;
char Per[],In[];
int Left[],Right[];
int rebuild(int L1,int R1,int L2,int R2)
{
if(L1>R1)return ;
int root=Per[L1]-'A'+;
int p=L2;
while(In[p]-'A'+!=root)p++;
int cnt=p-L2;
Left[root]=rebuild(L1+,L1+cnt,L2,p-);
Right[root]=rebuild(L1+cnt+,R1,p+,R2);
return root;
}
void dfs(int u)
{
//printf("%c",u+'A'-1);//先序
if(Left[u]!=)
dfs(Left[u]);
//printf("%c",u+'A'-1);//中序
if(Right[u]!=)
dfs(Right[u]);
printf("%c",u+'A'-);//后序
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%s%s",Per,In)!=EOF)
{
memset(Left,,sizeof(Left));
memset(Right,,sizeof(Right));
int n=strlen(Per);
rebuild(,n-,,n-);
dfs(Per[]-'A'+);
printf("\n");
}
return ;
}

UVa 536 Tree Recovery(二叉树后序遍历)的更多相关文章

  1. 二叉树后序遍历的非递归算法(C语言)

    首先非常感谢‘hicjiajia’的博文:二叉树后序遍历(非递归) 这篇随笔开启我的博客进程,成为万千程序员中的一员,坚持走到更远! 折磨了我一下午的后序遍历中午得到解决,关键在于标记右子树是否被访问 ...

  2. POJ 2255 Tree Recovery——二叉树的前序遍历、后序遍历、中序遍历规则(递归)

    1.前序遍历的规则:(根左右) (1)访问根节点 (2)前序遍历左子树 (3)前序遍历右子树 对于图中二叉树,前序遍历结果:ABDECF 2.中序遍历的规则:(左根右) (1)中序遍历左子树 (2)访 ...

  3. LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard

    题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...

  4. [Leetcode] Binary tree postorder traversal二叉树后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  5. UVa 536 Tree Recovery | GOJ 1077 Post-order (习题 6-3)

    传送门1: https://uva.onlinejudge.org/external/5/536.pdf 传送门2: http://acm.gdufe.edu.cn/Problem/read/id/1 ...

  6. lintcode.68 二叉树后序遍历

    二叉树的后序遍历    描述 笔记 数据 评测 给出一棵二叉树,返回其节点值的后序遍历. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返 ...

  7. UVa 548 Tree【二叉树的递归遍历】

    题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...

  8. 剑指Offer的学习笔记(C#篇)-- 平衡二叉树(二叉树后序遍历递归详解版)

    题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 一 . 题目分析 首先要理解一个概念:什么是平衡二叉树,如果某二叉树中任意的左右子树深度相差不超过1,那么他就是一颗平衡二叉树.如下图: 所以 ...

  9. 数据结构实验之求二叉树后序遍历和层次遍历(SDUT 2137)

    Problem Description 已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历和层序遍历. Input 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据. ...

随机推荐

  1. SpringBoot配置发送邮件

    一.导入jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  2. centos磁盘空间重新分配

    将/home目录压缩一部分空间到/ ref: https://serverfault.com/a/811124/434124 https://stackoverflow.com/a/19969471/ ...

  3. mysql5.7.21免安装版配置步骤

    1. 下载mysql5.7.21 地址https://dev.mysql.com/downloads/mysql/ 2. 解压缩 任何文件夹都行,为了避免放在系统盘,我放到了E盘,目录为E:\Prog ...

  4. css:关于position和float

    在CSS中,我们是通过定位属性position来进行定位的,具体它有如下几个属性值.常见的属性有如下几个: absolute  生成绝对定位的元素,相对于static定位以外的第一个父元素进行定位.元 ...

  5. Eclipse SVN文件冲突及不能直接提交情况

    下图为Eclipse SVN使用过程中存在文件冲突的情形. 以下是三种冲突情形及相应解决办法: 1.简单的文件版本冲突 情形:A改变了文件的头部,B改变了文件的尾部,如果两者改动互不影响,SVN可以智 ...

  6. Linux命令:ssh-add

    ssh-add帮助 SSH-ADD() BSD General Commands Manual SSH-ADD() NAME ssh-add — adds private key identities ...

  7. App.js实现使用js开发app的应用,此文是中文文档

    在阅读前,在此说明下,本人英文一直不好,所以该文档是借助翻译工具翻译的,阅读起来可能有点不好,请各位谅解,哪位大神有标准的中文文档请分享下 Github下载地址:https://github.com/ ...

  8. [PHP]PHPOffice/PHPExcel数据导入方法

    ------------------------------------------------------------------------------------ /** * PHPExcel数 ...

  9. 限制ssh登录ip和系统用户

    一般对于安全性要求比较高的系统,限制ssh登录的源ip地址和可以登录的系统账户是非常有必要的,ssh登录的源地址和可以登录的系统账户的设置在sshd的配置文件/etc/ssh/sshd_config中 ...

  10. java的特点

    java是一种跨平台.适合于分布式计算机环境的面向对象编程语言.具有以下特性:简单性.面向对象.分布性.解释性.可靠.安全.平台无关.可移植性.高性能.多线程.动态性等特点. 面向过程和面向对象可以用 ...