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. es6初级之解构----之一

    1. 访问对象属性 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  2. Delphi TQuery 的Locate用法

    Help里的解释 function Locate(const KeyFields: String; const KeyValues: Variant; Options: TLocateOptions) ...

  3. Java面试题_简答题

    作为一个大三在校生,很快就要去实习了,但总感觉自己连一个刚入门的菜鸟都不如,哎.发现自己连那个程序员的门槛都还没进,有点小伤心,不过伤心没用,努力向前才是我们现在应该做的事情. 下面是我之前在学校所从 ...

  4. Linux 中的定时处理 cron服务

    cron服务 在LINUX中,周期执行的任务一般由cron这个守护进程来处理 当安装完操作系统后默认会安装此服务工具并且会自动启动crond,该进程会每分钟定期检查是否有要执行的任务,若有则执行. c ...

  5. Grafana分析Nginx日志

    配置Groub by -Terms时报错,提示需要设置fielddata=true,报错内容大概如下: "Fielddata is disabled on text fields by de ...

  6. webpack+avalon+mmState打包方案

    终于到讲授如何整合avalon社区这个最强大的组件,基于状态机的路由系统了! 基于状态机的路由系统,据我所知,目前世界上只有三款,angular社区的ui-router, 网易出品的stateman, ...

  7. 434. Number of Segments in a String

    原题: 434. Number of Segments in a String 解题: 刚看到题目时,觉得可以通过统计空格个数,但想想有可能会有多个空格的情况 思路: 一:遍历字符,if条件碰到非空格 ...

  8. java的特点

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

  9. zabbix 邮件报警 监控mysql主从

    1)设置邮件模板及邮件服务器 邮箱密码记得写授权密码 2)配置接受报警的邮箱 3)添加报警触发器 配置邮箱服务器 yum -y install mailx yum -y install sendmai ...

  10. php zip扩展的一些基本操作

    public function zip_test() { $zip_obj = new ZipArchive(); $res = $zip_obj->open('/data1/www/www.k ...