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 ...
随机推荐
- A Byte of Python (1)安装和运行
有两种方式构建软件设计:一种是把软件做得很简单以至于明显找不到缺陷:另一种是把它做得很复杂以至于找不到明显的缺陷. ——C.A.R. Hoare 获得人生中的成功需要的专注与坚持不懈多过天才与机会. ...
- 百度2017笔试题:寻找n个员工中未打卡的那一个
声明:图片来自网络,笔者只是试着做了一下,然后做个记录. 拿到这个题目的时候,笔者首先想到的是二分.两个数组,一个是全体员工的集合A:一个是缺少一人的集合B.对A,B排序,再对B进行二分,得到B的中间 ...
- 使用CarrierWave上传图片时,多版本文件名的统一
第一次使用CarrierWavewe做上传,不能不说,虽然Rails已经把上传变得超简单了,而CarrierWave则是把上传变成了一种享受,特别是做图片上传,现在这年代,图片展示平台已经不仅仅是电脑 ...
- 26_Json_Example
JSON 很通用的处理数据的工具,各个语言都可以使用. 这个App就是把一个网上的用JSON格式保存的数据拿下来,然后保存到字典中,显示出来,用iOS自己的方法. 一定要记得写最后的那个 task.r ...
- 微软的MCE(Media Center Edition 媒体中心)标准
Windows VISTA和Windows 7操作系统上,电脑遥控器01RN的强劲功能更是发挥得淋漓尽致,不仅可以单凭遥控器一键即实现“听歌.看碟.播放控制.曲目选择.照片欣赏.幻灯片播放.网络电影电 ...
- android中使用setVideoURI()播放视频
最近在做一个demo,要求播放视频,记录一下.使用的是VideoView控件,如果播放网络视频的话,视频应该是渐进流式的,格式嘛,大家应该都知道,一般是H.263或者H.264格式的扩展名为3gp或者 ...
- 命令查询分离原则Command-query separation principle
在UML和模式应用一书中,发送给Die的roll消息之后跟随着第二个消息getFaceValue用于提取其新的faceValue,特别是:roll()方法是void的,没有返回值,例如: public ...
- HDU 1851 A Simple Game
典型的尼姆博弈,在n对石子中,告诉你每堆的数目和每次从该堆最多可以取的数目,求最终谁将其取完. 题解:SG(i)=mi%(li+1),求异或值即可. #include <cstdio> i ...
- BZOJ 1023
program bzoj1023; uses math; ; maxn=; maxm=; type edge=record togo,next:longint; end; var n,m,cnt,in ...
- hadoop高速扫盲帖,从零了解hadoop
1.MapReduce理论简单介绍 1.1 MapReduce编程模型 MapReduce採用"分而治之"的思想,把对大规模数据集的操作,分发给一个主节点管理下的各个分节点共同完毕 ...