二叉树遍历问题

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 - 二叉树遍历(推荐)的更多相关文章

  1. C++ 二叉树遍历实现

    原文:http://blog.csdn.net/nuaazdh/article/details/7032226 //二叉树遍历 //作者:nuaazdh //时间:2011年12月1日 #includ ...

  2. python实现二叉树遍历算法

    说起二叉树的遍历,大学里讲的是递归算法,大多数人首先想到也是递归算法.但作为一个有理想有追求的程序员.也应该学学非递归算法实现二叉树遍历.二叉树的非递归算法需要用到辅助栈,算法着实巧妙,令人脑洞大开. ...

  3. 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历

    [二叉树遍历模版]前序遍历     1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...

  4. hdu 4605 线段树与二叉树遍历

    思路: 首先将所有的查询有一个vector保存起来.我们从1号点开始dfs这颗二叉树,用线段树记录到当前节点时,走左节点的有多少比要查询该节点的X值小的,有多少大的, 同样要记录走右节点的有多少比X小 ...

  5. poj2255 (二叉树遍历)

    poj2255 二叉树遍历 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descripti ...

  6. 二叉树遍历 C#

    二叉树遍历 C# 什么是二叉树 二叉树是每个节点最多有两个子树的树结构 (1)完全二叉树——若设二叉树的高度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第h层有叶子结点,并 ...

  7. 二叉树——遍历篇(递归/非递归,C++)

    二叉树--遍历篇 二叉树很多算法题都与其遍历相关,笔者经过大量学习.思考,整理总结写下二叉树的遍历篇,涵盖递归和非递归实现. 1.二叉树数据结构及访问函数 #include <stdio.h&g ...

  8. 二叉树遍历(flist)(二叉树,已知中序层序,求先序)

    问题 C: 二叉树遍历(flist) 时间限制: 1 Sec  内存限制: 128 MB提交: 76  解决: 53[提交][状态][讨论版][命题人:quanxing][Edit] [TestDat ...

  9. 二叉树遍历(flist)(已知中序和按层遍历,求先序 )

    问题 F: 二叉树遍历(flist) 时间限制: 1 Sec  内存限制: 128 MB提交: 11  解决: 9[提交][状态][讨论版][命题人:quanxing][Edit] [TestData ...

随机推荐

  1. HOOK API(二)—— HOOK自己程序的 MessageBox

    HOOK API(二) —— HOOK自己程序的 MessageBox 0x00 前言 以下将给出一个简单的例子,作为HOOK API的入门.这里是HOOK 自己程序的MessageBox,即将自己程 ...

  2. 驴吃胡萝卜问题——牛客/FEI

    一个商人骑一头驴要穿越1000公里长的沙漠,去卖3000根胡萝卜.已知驴一次性可驮1000根胡萝卜,但每走1公里又要吃掉1根胡萝卜.问:商人最多可卖出多少胡萝卜? 一个商人骑一头驴要穿越1000公里长 ...

  3. BZOJ 1044: [HAOI2008]木棍分割(二分答案 + dp)

    第一问可以二分答案,然后贪心来判断. 第二问dp, dp[i][j] = sigma(dp[k][j - 1]) (1 <= k <i, sum[i] - sum[k] <= ans ...

  4. Symfony命令行

    Available commands:  help                                  显示命令的帮助信息  list                           ...

  5. Linux软件间的依赖关系(转)

    Linux中的软件大部分是零碎的,其粒度比windows的小很多,软件之间的依赖关系很强烈,下面是自己的一些理解: 一.Linux中的软件依赖Linux中的软件依赖关系成一颗拓扑树结构,比如A直接或间 ...

  6. 扩展ASP.NET MVC HtmlHelper类

    在这篇帖子中我会使用一个示例演示扩展ASP.NET MVC HtmlHelper类,让它们可以在你的MVC视图中工作.这个示例中我会提供一个简单的方案生成Html表格. HtmlHelper类 Htm ...

  7. Delphi程序自删除的几种方法

    program Project1; uses SysUtils, windows; var f:textfile; a:string; begin a:=paramstr(); assignfile( ...

  8. 在SQL2005中部署CLR 程序集

    原文 在SQL2005中部署CLR 程序集 有关于CLR函数的用途和用法,请了解 SQL Server CLR 极速入门,启用.设计.部署.运行 http://www.yongfa365.com/It ...

  9. 用elasticsearch索引mongodb数据

    参照网页:单机搭建elasticsearch和mongodb的river 三个步骤: 一,搭建单机replicSet二,安装mongodb-river插件三,创建meta,验证使用 第一步,搭建单机m ...

  10. CF 192 Div2

    A.Cakeminator 暴搞之,从没有草莓覆盖的行.列遍历 char map[30][30]; int vis[30][30]; int hang[30],lie[30]; int main() ...