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. Linux zookeeper 单机安装

    Zookeeper(端口2181) 下载地址 http://mirror.bit.edu.cn/apache/zookeeper/ 解压到/usr/local目录 >tar -zxvf zook ...

  2. Spark/Storm/Flink

    https://www.cnblogs.com/yaohaitao/p/5703288.html  Spark Streaming与Storm的应用场景 对于Storm来说:1.建议在那种需要纯实时, ...

  3. es6 初级之let

    1.在es6 中,定义变量使用 let. 1.1 var定义变量: <!DOCTYPE html> <html lang="en"> <head> ...

  4. oracle第一天笔记

    Oracle体系结构: 数据库   ---->  实例(orcl) --->  表空间(逻辑单位)(用户)  ---> 数据文件(物理单位) 地球     ---->   国家 ...

  5. ADO 动态链接数据库

    ADO(ActiveX Data Objects)是基于组件的数据库编程接口,它是一个和编程语言无关的COM组件系统. 下面内容转载自:http://www.vckbase.com/document/ ...

  6. mysql分库分区分表

    分表: 分表分为水平分表和垂直分表. 水平分表原理: 分表策略通常是用户ID取模,如果不是整数,可以首先将其进行hash获取到整. 水平分表遇到的问题: 1. 跨表直接连接查询无法进行 2. 我们需要 ...

  7. Haskell语言学习笔记(85)Async

    安装 async $ cabal install async async-2.2.1 installed async / wait / concurrently async :: IO a -> ...

  8. springboot 整合redis redis工具类

    一步 : pom中引入相关依赖 <!-- 引入 redis 依赖 --> <dependency> <groupId>org.springframework.boo ...

  9. Python自动化运维开发实战 二、Python基本用法

    导语: Python编程博大精深,知识点众多,需要先整体上了解python的一些基本用法之后再去对每一个知识点细细研究,这样学习的速度会快很多.所以下面就先看一些python事先需要知道的基本知识. ...

  10. Zabbix监控系统配置

    1.Zabbix是一个基于WEB界面的提供分布式系统监控的企业级的开源解决方案 Zabbix能监视各种网络参数,保证服务器系统的安全稳定的运行,并提供灵活的通知机制以让SA快速定位并解决存在的各种问题 ...