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

题意:给了二叉树的前序、中序,求后序

思路:

前序遍历:根-->左孩子-->右孩子
中序遍历:左孩子-->根-->右孩子
后序遍历:左孩子-->右孩子-->根
所谓的前中后指的是根的位置,而左右孩子顺序是不变的。 例如已知前序遍历是DBACEGF,中序遍历是ABCDEFG,那么由前序遍历先根,可知道D是树的根,再看在中序遍历中D左边是ABC,所以可知道ABC一定在D的左子树上,而EFG在D的右子树上。
那么前序遍历为BAC,中序遍历为ABC,所以B为根,在中序遍历中A在B的左边,C在B的右边,所以A为B的左孩子,C为B的有孩子。
以此类推递归下去。
递归找到,代码很简单,但是要明白怎么递归:

参考博客:http://www.cnblogs.com/-sunshine/archive/2012/07/24/2606341.html

学到一个新函数:

strchr:用来找s2中首次出现s1[0]的位置,返回位置指针,若不存在则返回NULL

 #include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; void print(int n,char *s1,char *s2,char *s)
{
if(n<=)
return;
int x=strchr(s2,s1[])-s2;
//strchr用来找s2中首次出现s1[0]的位置,返回位置指针,若不存在则返回NULL
print(x,s1+,s2,s);//得到左子树
print(n--x,s1+x+,s2+x+,s+x);//得到右子树
s[n-]=s1[];//最后一个是根
}
int main()
{
char s1[],s2[],s3[];
memset(s1,'\0',sizeof(s1));
memset(s2,'\0',sizeof(s2));
memset(s3,'\0',sizeof(s3));
while(~scanf("%s %s",s1,s2))
{
print(strlen(s1),s1,s2,s3);
s3[strlen(s1)]='\0';//没有这个输出CDABGED不是CDAB
printf("%s\n",s3);
}
return ;
}

POJ-2255-Tree Recovery-求后序的更多相关文章

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

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

  2. UVa 536 Tree Recovery(二叉树后序遍历)

    Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...

  3. POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)

    链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...

  4. POJ 2255 Tree Recovery(根据前序遍历和中序遍历,输出后序遍历)

    题意:给出一颗二叉树的前序遍历和中序遍历的序列,让你输出后序遍历的序列. 思路:见代码,采用递归. #include <iostream> #include <stdio.h> ...

  5. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  6. Poj 2255 Tree Recovery(二叉搜索树)

    题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...

  7. POJ 2255 Tree Recovery 二叉树的遍历

    前序和中序输入二叉树,后序输出二叉树:核心思想只有一个,前序的每个根都把中序分成了两部分,例如 DBACEGF ABCDEFG D把中序遍历的结果分成了ABC和EFG两部分,实际上,这就是D这个根的左 ...

  8. POJ 2255 Tree Recovery 二叉树恢复

    一道和Leetcode的一道题目基本上一样的题目. 给出前序遍历和中序遍历序列,要求依据这些信息恢复一颗二叉树的原貌,然后按后序遍历序列输出. Leetcode上有给出后序和中序,恢复二叉树的. 只是 ...

  9. poj 2255 Tree Recovery 分治

    Tree Recovery Description Little Valentine liked playing with binary trees very much. Her favorite g ...

  10. poj 2255 Tree Recovery(求后序遍历,二叉树)

    版权声明:本文为博主原创文章,未经博主同意不得转载.vasttian https://blog.csdn.net/u012860063/article/details/37699219 转载请注明出处 ...

随机推荐

  1. 【BZOJ 3569】DZY Loves Chinese II

    题面 Description 神校XJ之学霸兮,Dzy皇考曰JC. 摄提贞于孟陬兮,惟庚寅Dzy以降. 纷Dzy既有此内美兮,又重之以修能. 遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图 ...

  2. Spring Boot 2.X 对 web 的开发支持(二)

    Spring Boot 2.X 对 web 的支持开发 上章节的 Spring Boot 的入门案例,我们感受到 Spring Boot 简单的配置即可运行项目. 今天了解 Spring Boot 对 ...

  3. __str__方法

    """str()就是可以自定义输出返回值,必须是str字符串""" class Dog: def __init__(self, name): ...

  4. thinkphp 类的扩展

    ThinkPHP的类库主要包括公共类库和应用类库,都是基于命名空间进行定义和扩展的.只要按照规范定义,都可以实现自动加载. 大理石平台价格 公共类库 公共类库通常是指ThinkPHP/Library目 ...

  5. bzoj1042题解

    [题意分析] 有q个询问,每次询问求一个只有四个物品的背包方案数. [解题思路] 先计算出所有面值硬币的无限背包方案数. 考虑容斥,每个限制表示选取大于di个面值ci的硬币,总方案数=0限制方案数-1 ...

  6. Css实现Div在页面上垂直居中显示

    方法一 <html>    <head>        <title>垂直居中</title>        <style type=" ...

  7. P1624 单词缩写

    P1624 单词缩写 题目描述 树树发现好多计算机中的单词都是缩写,如GDB是全称Gnu DeBug的缩写.但是,有时候缩写对应的全称会不固定,如缩写LINUX可以理解为: (1) LINus’s U ...

  8. flutter SnackBar

    无法弹出 snackbar时

  9. springboot 项目普通类中调用mapper或service接口

    1.该类使用@Component注解 2.添加一个本类类型的静态字段 3.创建一个初始化方法,贴上@PostConstruct 标签,用于注入bean 4.创建方法调用mapper或service接口 ...

  10. Netty环境安装配置

    本章中介绍的Netty开发环境的安装及配置; 这个一系列教程示例的Netty最低要求只有两个:最新版本的Netty 4.x和JDK 1.6及更高版本. 最新版本的Netty在项目下载页面中可找到:ht ...