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. CDH 下线节点

    rm -rf /var/web/com/public  杜绝这种删除方式 停止集群下线的节点 步骤: 1 停止主机上的角色 2 解除授权 3 从集群中删除 4 为了避免数据丢失,必须一台一台的进行,如 ...

  2. Perl 运算符

    Perl 运算符 运算符是一种告诉编译器执行特定的数学或逻辑操作的符号,如: 3+2=5. Perl 语言内置了丰富的运算符,我们来看下常用的几种: 算术运算符 比较运算符 逻辑运算符 赋值运算符 位 ...

  3. 「题解」:07.16NOIP模拟T1:礼物

    问题 A: 礼物 时间限制: 1 Sec  内存限制: 256 MB 题面 题目描述 夏川的生日就要到了.作为夏川形式上的男朋友,季堂打算给夏川买一些生 日礼物. 商店里一共有种礼物.夏川每得到一种礼 ...

  4. Python 爬取12306火车票

    获取火车站 stations.py #import certifi #import urllib3 import re import requests from pprint import pprin ...

  5. VS开发工具的常用插件【转载】

    文章转载自https://www.cnblogs.com/huntergu/p/8337266.html

  6. 剑指offer——30栈的压入、弹出序列

    题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...

  7. 打包的@font-face包

    在网页中使用 @font-face 规则嵌入字体,前提是可以从你的网站或第三方 Web 服务器下载到相应的字体.以这种方式提供的字体,会在使用该字体的页面第一次加载时被浏览器下载并缓存起来,以后就不用 ...

  8. Java学习之JVM、JRE、JDK联系与区别

    JVM,全称是Java Virtual Machine,翻译为Java虚拟机: JRE,全称是Java Runtime Environment,翻译为Java运行时环境: JDK,全称是Java De ...

  9. UVA 12304 /// 圆的综合题 圆的模板

    题目大意: ①给出三角形三个点,求三角形外接圆,求外接圆的圆心和半径. ②给出三角形三个点,求三角形内接圆,求内接圆的圆心和半径. ③给出一个圆,和一个点,求过该点的圆的切线与x轴的夹角(0<= ...

  10. 学无止境的CSS(xHTML+CSS技巧教程资源大全)

    本文里面收集一些有关CSS的技巧.教程.工具和观点等,其中一些你也许早就运用的炉火纯青,也可能有的你听都没听说过.不管是新手还是高手,大家都继续学习吧. 一,Web 标准 要玩游戏,就得先了解规则.要 ...