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 转载请注明出处 ...
随机推荐
- table 表头不动,tbody滚动对齐
http://www.imaputz.com/cssStuff/bigFourVersion.html# https://blog.csdn.net/yiifaa/article/details/52 ...
- 28. string类中方法练习
1. 自己写trim方法 public class Demo3 { public static void main(String[] args) { System.out.println(myTrim ...
- leetcode-第11场双周赛-5088-等差数列中缺失的数字
题目描述: 自己的提交: class Solution: def missingNumber(self, arr: List[int]) -> int: if len(arr) == 2: re ...
- re.match与re.search的区别
re.match与re.search的区别 re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None:而re.search匹配整个字符串,直到找到一个匹配. 实 ...
- 一起看下Apache1.3.22版本改进和修正了啥?
Apache 1.3.20 - 1.3.22主要改进: 安全弱点: 1.在Apache1.3.20的win32平台上发现了一个漏洞.如果客户端发送一个非常长的URI可能导致用目录列表来代替缺省主页.4 ...
- 直接用编译器按ctrl+F5运行和双击运行结果不一样
是因为进程权限的问题,需要添加下面的代码: BOOL EnableDebugPrivilege() { HANDLE hToken; BOOL fOk=FALSE; if(OpenProcessTok ...
- JVM内核-原理、诊断与优化学习笔记(五):GC参数
文章目录 堆的回顾 串行收集器 并行收集器 ParNew(par-并行的缩写,new-新生代,所以只是新生代并行) Parallel收集器 参数设置 -XX:MaxGCPauseMills -XX:G ...
- 夏令营501-511NOIP训练18——高二学堂
传送门:QAQQAQ 题意:给你一个数$n$,把它拆分成至多$k$个正整数,使得这些数的和等于$n$且每一个正整数的个数不能超过$4$ 拆分的顺序是无序的,但取出每一个数方案是不同的(例如我要拆$1$ ...
- java锁分析
import java.util.concurrent.TimeUnit; class Phone//Phone.java ---> Phone.class Class.forName(); { ...
- HTML_表单标签
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...