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. C++中map用法详解《转》

    Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作! . map最基本的构造函数: map<string ...

  2. Django--templates(模板层)

    模板语法: """ 模板语法: 变量:{{}} 1.深度查询 句点符 2.过滤器 {{value|filter_name:参数}} 标签:{% %} "&quo ...

  3. HTTP、TCP、IP协议常见面试题

    前言:在看面试题之前,先了解一下基本定义. HTTP.TCP.IP协议基本定义 HTTP: (HyperText Transport Protocol)是超文本传输协议的缩写,它用于传送WWW方式的数 ...

  4. 遍历DOM树,理解更新范围

    在JavaScript中,如果需求对多个元素重复进行同样的操作,就需要写一个循环来遍历选中的所有元素. 在jQuery中,当选择器返回了多个元素时,可以使用一个方法来更新所有的元素,不再需要使用循环. ...

  5. week5 0.1 安装materializecss

    用ATOM打开项目 App是什么呢?就是App.js 我们将不需要的删掉 用一下materialize(类似bootstrap的东西) 官网https://materializecss.com/ 想用 ...

  6. C++ 关于 CMFCPropertyGridCtrl 的使用方法 之二 (原创)

    接上一节所讲,这一节咱们重点讲一下CMFCPropertyGridCtrl 所支持的数据表格的建立过程 在上一节中,咱们已经了解到了 CMFCPropertyGridCtrl  是要用到实例函数:Ad ...

  7. C# 图像处理:复制屏幕到内存中,拷屏操作

    /// <summary> /// 复制屏幕到内存中 /// </summary> /// <returns>返回内存流</returns> publi ...

  8. [译]CQRS介绍

    以下内容均为看完原文后自己的理解.并非一字一句翻译,会尽量保持原文意思. 什么是 CQRS: CQRS 意思就是命令查询职责分离(Command Query Responsibility Segreg ...

  9. 问题描述: fatal error: 'XCTest/XCTest.h' file not found

    #import 解决方法:在报错的Target中的Building settings中FRAMEWORK_SEARCH_PATHS添加$(PLATFORM_DIR)/Developer/Library ...

  10. C# 模拟多线程下载文件

    原地址:http://www.cnblogs.com/Opiece/p/4803836.html 客户端 public static string strContent = ""; ...