poj2255 (二叉树遍历)
poj2255
二叉树遍历
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
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 Specification
The input file 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 Specification
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 题解:
提示:二叉树遍历而已。。。给出前序和中序,求后序
思路:
1、前序遍历的第一个字母是根
2、在中序遍历的字母串中找出 根字母,那么根字母左右两边的就分别是它的左、右子树
3、利用递归复原二叉树(把子树看作新的二叉树)
4、后序遍历特征:后序遍历字母串 自右至左
5、输出后序遍历时,只需按4的顺序从左到右排列,再倒置输出即可
看了一晚上二叉树,指针什么的真的不会,偶然发现了一个好东西,用的数组,精简了半天。很容易懂
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char mid[];
char pre[];
int n = -; void juge(int i, int j)
{
int k;
if(i > j) return;
n++;
for(k = i; k <= j; k++)
{
if(pre[n] == mid[k])
break;
}
juge(i, k - );//递归复原二元树
juge(k + , j);
printf("%c", mid[k]); }
int main()
{
while(scanf("%s%s", pre, mid) == )
{
juge(, strlen(pre) - );//用的漂亮
printf("\n");
n = -;
}
return ;
}
poj2255 (二叉树遍历)的更多相关文章
- C++ 二叉树遍历实现
原文:http://blog.csdn.net/nuaazdh/article/details/7032226 //二叉树遍历 //作者:nuaazdh //时间:2011年12月1日 #includ ...
- python实现二叉树遍历算法
说起二叉树的遍历,大学里讲的是递归算法,大多数人首先想到也是递归算法.但作为一个有理想有追求的程序员.也应该学学非递归算法实现二叉树遍历.二叉树的非递归算法需要用到辅助栈,算法着实巧妙,令人脑洞大开. ...
- 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历
[二叉树遍历模版]前序遍历 1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...
- hdu 4605 线段树与二叉树遍历
思路: 首先将所有的查询有一个vector保存起来.我们从1号点开始dfs这颗二叉树,用线段树记录到当前节点时,走左节点的有多少比要查询该节点的X值小的,有多少大的, 同样要记录走右节点的有多少比X小 ...
- D - 二叉树遍历(推荐)
二叉树遍历问题 Description Tree Recovery Little Valentine liked playing with binary trees very much. Her ...
- 二叉树遍历 C#
二叉树遍历 C# 什么是二叉树 二叉树是每个节点最多有两个子树的树结构 (1)完全二叉树——若设二叉树的高度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第h层有叶子结点,并 ...
- 二叉树——遍历篇(递归/非递归,C++)
二叉树--遍历篇 二叉树很多算法题都与其遍历相关,笔者经过大量学习.思考,整理总结写下二叉树的遍历篇,涵盖递归和非递归实现. 1.二叉树数据结构及访问函数 #include <stdio.h&g ...
- 二叉树遍历(flist)(二叉树,已知中序层序,求先序)
问题 C: 二叉树遍历(flist) 时间限制: 1 Sec 内存限制: 128 MB提交: 76 解决: 53[提交][状态][讨论版][命题人:quanxing][Edit] [TestDat ...
- 二叉树遍历(flist)(已知中序和按层遍历,求先序 )
问题 F: 二叉树遍历(flist) 时间限制: 1 Sec 内存限制: 128 MB提交: 11 解决: 9[提交][状态][讨论版][命题人:quanxing][Edit] [TestData ...
随机推荐
- 重载(Overloading)以及模板(Template)
继续<C++ premier plus>的学习 (1)函数重载,通俗来说,就是相同的函数名字名下,存在多个函数,要使得这成立,各个同名函数必须形参列表(也称为"签名", ...
- haffman树c实现
#include<stdio.h>#include<stdlib.h>#include<string.h>#define N 100#define M 2*N-1t ...
- UVa 10256 凸包简单应用
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 小菜学习Lucene.Net(更新3.0.3版本使用)
花了两天的时间研究了下Lucene.Net 发现确实挺好玩.... 最新版本是3.0.3 (最后更新时间2012-10) 可惜3.0.3版本的Lucene.net无法和盘古分词 (最新版为2.3.1. ...
- Configuration所有配置简介
// 内存缓存的设置选项 (最大图片宽度,最大图片高度) 默认当前屏幕分辨率 // .memoryCacheExtraOptions(480, 800) // 硬盘缓存的 ...
- POJ 3384 Feng Shui 半平面交
题目大意:一个人很信"Feng Shui",他要在房间里放两个圆形的地毯. 这两个地毯之间可以重叠,可是不能折叠,也不能伸到房间的外面.求这两个地毯可以覆盖的最大范围.并输出这两个 ...
- Facebook Hacker Cup 2015 Round 1--Homework(筛选法求素数)
题意:给定A,B,K(A<=B)三个数,问在[A,B]范围内的数素数因子个数为K的个数. 题解:典型的筛选法求素数.首先建立一个保存素数因子个数的数组factorNum[],以及到n为止含有素数 ...
- Toast的使用具体解释
Android中提供一种简单的Toast消息提示框机制,能够在用户点击了某些button后,提示用户一些信息,提示的信息不能被用户点击,Toast的提示信息依据用户设置的显示时间后自己主动消失.Toa ...
- Linux 开机自检的设置(tune2fs和fsck)
tune2fs和fsck的用法 tune2fs--调整ext2/ext3文件系统特性的工具. -l <device> 查看文件系统信息 -c <count> 设置强制自检的 ...
- iOS 独立开发记录(下)
侧边菜单栏 查看Github上相关实现,一开始选择的是SlideMenuControllerSwift,后来决定更改为自定义,使用更简洁的方式. 分离 分离之前的SliderMeanControlle ...