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 ...
随机推荐
- [github] 创建个人网页
创建 github.io 的个人子域名网页,其实非常简单,只需要两步: 1)创建名为 username.github.io 的代码仓库 2)把网页代码上传到代码库中 在 username.github ...
- haffman树c实现
#include<stdio.h>#include<stdlib.h>#include<string.h>#define N 100#define M 2*N-1t ...
- zoj 1760 floyd构图+Dinic最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 #include <cstdio> #includ ...
- DOM解析原理
用于处理XML文档的DOM元素属性 childNodes:返回当前元素所有子元素的数组: firstChild:返回当前元素的第一个下级子元素: lastChild:返回当前元素的最后一个子元素: n ...
- Linux 相关scsi命令
Linux 相关scsi命令 由于前段时间存储扩容,对存储操作较多,下面记录了常用的操作: lsscsi命令:显示scsi设备信息 #lsscsi [0:0:0:2] disk IBM ...
- Delphi图像处理 -- RGB与HSL转换
阅读提示: <Delphi图像处理>系列以效率为侧重点,一般代码为PASCAL,核心代码采用BASM. <C++图像处理>系列以代码清晰,可读性为主,全部使用C ...
- Leetcode - Reverse Words
比起POJ弱爆了一题,从后往前扫描一遍,O(n)时间,仅仅要注意各种极端情况就可以,不明确通过率为什么仅仅有13%. #include<iostream> #include<stri ...
- @property的特性
@property还有一些关键字,它们都是有特殊作用的,比如上述代码中的nonatomic,strong: 1 2 @property(nonatomic,strong) NSString *carN ...
- Java基础知识强化之IO流笔记20:FileOutputStream写出数据实现换行和追加写入
1. 如何实现数据的换行? (1) package com.himi.fileoutputstream; import java.io.FileNotFoundException; import j ...
- service redis does not support chkconfig的解决办法
原文链接: http://my.oschina.net/maczhao/blog/322931 问题解决办法如下: 必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中: # ch ...