POJ-2255-Tree Recovery-求后序
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
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
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-求后序的更多相关文章
- POJ 2255 Tree Recovery——二叉树的前序遍历、后序遍历、中序遍历规则(递归)
1.前序遍历的规则:(根左右) (1)访问根节点 (2)前序遍历左子树 (3)前序遍历右子树 对于图中二叉树,前序遍历结果:ABDECF 2.中序遍历的规则:(左根右) (1)中序遍历左子树 (2)访 ...
- UVa 536 Tree Recovery(二叉树后序遍历)
Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...
- POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)
链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...
- POJ 2255 Tree Recovery(根据前序遍历和中序遍历,输出后序遍历)
题意:给出一颗二叉树的前序遍历和中序遍历的序列,让你输出后序遍历的序列. 思路:见代码,采用递归. #include <iostream> #include <stdio.h> ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- Poj 2255 Tree Recovery(二叉搜索树)
题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...
- POJ 2255 Tree Recovery 二叉树的遍历
前序和中序输入二叉树,后序输出二叉树:核心思想只有一个,前序的每个根都把中序分成了两部分,例如 DBACEGF ABCDEFG D把中序遍历的结果分成了ABC和EFG两部分,实际上,这就是D这个根的左 ...
- POJ 2255 Tree Recovery 二叉树恢复
一道和Leetcode的一道题目基本上一样的题目. 给出前序遍历和中序遍历序列,要求依据这些信息恢复一颗二叉树的原貌,然后按后序遍历序列输出. Leetcode上有给出后序和中序,恢复二叉树的. 只是 ...
- poj 2255 Tree Recovery 分治
Tree Recovery Description Little Valentine liked playing with binary trees very much. Her favorite g ...
- poj 2255 Tree Recovery(求后序遍历,二叉树)
版权声明:本文为博主原创文章,未经博主同意不得转载.vasttian https://blog.csdn.net/u012860063/article/details/37699219 转载请注明出处 ...
随机推荐
- cehsi
weibo https://oapi.dingtalk.com/robot/send?access_token=8c9ef96c99925383347c5f9f733ad6b8579c3f8ad072 ...
- Redis缓存数据库常见操作
Jedis的最为常见的操作.主要包括常用的列表(list).集合(set).有序集合(sorted set).哈希表(hash)等数据结构,以及其他特性支持. 参考资料:http://hello-ni ...
- Android中的Parcel机制(上)
一.先从Serialize说起 我们都知道JAVA中的Serialize机制,译成串行化.序列化--,其作用是能将数据对象存入字节流当中,在需要时重新生成对象.主要应用是利用外部存储设备保存对象状态, ...
- C++之静态(static)
一.静态数据成员与静态成员函数 二.从内存角度看静态数据成员 三.从this指针谈静态成员函数 四.注意事项 五.补充说明 1.<静态>课程评论: 静态成员是类的成员,不是对象的成员: 静 ...
- Java-Class-@I:org.springframework.validation.annotation.Validated
ylbtech-Java-Class-@I:org.springframework.validation.annotation.Validated 1.返回顶部 2.返回顶部 1. package ...
- C语言进阶学习第三章
以下记录动态内存分配: 1.malloc和free malloc和free分别用于执行动态内存分配和释放.这些函数维护一个可用内存池,当一个程序需要一些内存时,调用malloc函数,malloc从内存 ...
- 重启集群的时候发现HBase的HRegionServer 服务启动失败
今天在测试环境下的集群重启了下,启动Hbase的时候报错: $ sh start-hbase.sh starting master, logging to /home/hadoop/hbase-0.9 ...
- java 数组中的数值反转输出
package com.test; /** *数组元素反转 * */ public class ArraySwap { public static void main(String[] args) { ...
- 【转】WebResource实现在自定义控件中内嵌JS文件
在类库中的资源 其他项目中要使用 需要嵌入才行 参考文献:WebResource实现在自定义控件中内嵌JS文件 1. WebResource简介 ASP.NET(1.0/1.1)给我们提供了一个开发 ...
- shell 命令 查找命令find,grep
1.find 查找文件 [ find -name 文件名 ] 在当前目录及子目录中找这个文件 [ find -iname 文件名 ] 在当前目录及子目录中找这个文件,不区分大小写 [ find -na ...