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 转载请注明出处 ...
随机推荐
- java创建一个空白zip
String zipath = localpath+zipname+".zip"; public static void createNewzip(String zipath) t ...
- 【FJWC2018】最大真因数
题面 Description 一个合数的真因数是指这个数不包括其本身的所有因数, 例如 6 的正因数有1, 2, 3, 6,其中真因数有 1, 2, 3. 一个合数的最大真因数则是这个数的所有真因数中 ...
- leetcood学习笔记-404-左叶子之和
题目描述: 方法一:递归 class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: if not root: retur ...
- 51nod 1627 瞬间移动(组合数学)
传送门 解题思路 因为每次横纵坐标至少\(+1\),所以可以枚举走的步数,枚举走的步数\(i\)后剩下的就是把\(n-1\)与\(m-1\)划分成\(i\)个有序正整数相加,所以用隔板法,\(ans= ...
- params拦截器
1. params拦截器首先给action中的相关参数赋值,如id 2. prepare拦截器执行prepare方法,prepare方法中会根据参数,如id,去调用业务逻辑,设置model对象 ...
- mac 安装配置使用 mongoldb
mac 安装配置使用 mongoldb 安装和配置 brew install mongos brew install mongo # 密码就是用户的密码 # 配置数据文件 //如果不配置会出现错误62 ...
- function attributes, MDK
The keyword format is either of the following: __attribute__((attribute1, attribute2, ...)) __attrib ...
- Day 13 : 函数递归,
从前有有座山,山里有座庙,庙里有个老和尚给小和尚们讲故事,讲的什么呀,讲的是,从前有有座山,山里有座庙,庙里有个老和尚给小和尚们讲故事,讲的什么呀?讲的是?...... 递归:1.一个函数再内部调用了 ...
- seo具备的条件
对于SEO这行业,许多想学习这一行,但是并非每一个人都有这样才能.因为SEO份这行靠是真材实料并非虚拟人才.现在找到高工资的SEO人才需要 有三年以上经验,熟悉PHP.html.asp.java等等这 ...
- WebService接口测试