D - 二叉树遍历(推荐)
二叉树遍历问题
Description
Tree Recovery
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
题目大意:
已知二叉树的先序和中序,求后序。
分析:
用递归对二叉树进行遍历。
代码:
#include <iostream>
#include <cstring>
using namespace std; char pre[],mid[]; void T(int p1,int p2,int q1,int q2,int k ) //递归进行遍历
{
if (p1>p2)
return;
for (k=q1;mid[k]!=pre[p1];++k);
T(p1+,p1+k-q1,q1,k-,);
T(p1+k-q1+,p2,k+,q2,);
cout<<mid[k];
} int main()
{
while(cin>>pre>>mid)
{
int l=strlen(pre)-;
T(,l,,l,);
cout<<endl;
}
return ;
}
心得:
二叉树问题看起来很容易,但要对先序、中序、以及后序有了解,才能更好的做二叉树的题目。二叉树问题无非就是两序找一序,一般都是用递归方法来实现,要好好学好递归,递归应用的方面有很多。
D - 二叉树遍历(推荐)的更多相关文章
- 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小 ...
- poj2255 (二叉树遍历)
poj2255 二叉树遍历 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descripti ...
- 二叉树遍历 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 ...
随机推荐
- Struts学习之值栈的理解
转自:http://blog.csdn.net/hanxuemin12345/article/details/38559979 页面一个请求发送过来,依次经过一系列拦截器(处理公共部分,如:往数据中心 ...
- 手机浏览器下IScroll中click事件
产品的h5页面几乎都使用了iscroll插件,如果a标签在iscroll里,在部分手机浏览器中会出现无法点击的情况,不管是绑定click事件还是使用a标签的href属性.href属性偶尔还会能点击,c ...
- win7系统无线 VirtualBox rehat 32位linux 下 host-only模式相互通信及上网 配置
虚拟机环境:virtualBox虚拟环境 redhat 32位 主机环境 : win7 无线路由 模式: host-only win7下nat模式原先的设置基于 有网线连接的情形下,在使用了无线路由之 ...
- poj 1041 John's trip 欧拉回路
题目链接 求给出的图是否存在欧拉回路并输出路径, 从1这个点开始, 输出时按边的升序输出. 将每个点的边排序一下就可以. #include <iostream> #include < ...
- 射频识别技术漫谈(24)——ISO15693的防冲突与传输协议
遵守ISO15693协议的电子标签都有一个8字节共64bit的全球唯一序列号(UID),这个UID一方面可以使全球范围内的标签互相区别,更重要的是可以在多标签同时读写时用于防冲突.8字节UID按权重从 ...
- linux 查看文件系统类型
1. mount [op@TIM ~]$ mount/dev/mapper/vg_tim-lv_root on / type ext4 (rw)proc on /proc type proc (rw) ...
- hive 0.10 0.11新增特性综述
我们的hive版本升迁经历了0.7.1 -> 0.8.1 -> 0.9.0,并且线上shark所依赖的hive版本也停留在0.9.0上,在这些版本上有我们自己的bug fix patch和 ...
- c基础知识复习
C的发展历程 C原本是为了开发UNIX操作系统而设计的语言:如此说,应该C比UNIX更早问世,而事实并非如此,最早的UNIX是由汇编写的: C语言本来是美国人开发的,解读C的声明,最好还是用英语来读: ...
- ThinkPHP - 登录模块,核心代码
/** * 登录成功 * @return [type] [description] */ public function checkLogin($data) { $user = M($this-> ...
- MVC DI
using System;using Microsoft.Practices.Unity; public class BizInstanceFactory { private static reado ...