二叉树遍历问题

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:

  1. D
  2. / \
  3. / \
  4. B E
  5. / \ \
  6. / \ \
  7. A C G
  8. /
  9. /
  10. 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

  1. DBACEGF ABCDEFG
  2. BCAD CBAD

Sample Output

  1. ACBFGED
  2. CDAB

题目大意:

已知二叉树的先序和中序,求后序。

分析:

用递归对二叉树进行遍历。

代码:

  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. char pre[],mid[];
  6.  
  7. void T(int p1,int p2,int q1,int q2,int k ) //递归进行遍历
  8. {
  9. if (p1>p2)
  10. return;
  11. for (k=q1;mid[k]!=pre[p1];++k);
  12. T(p1+,p1+k-q1,q1,k-,);
  13. T(p1+k-q1+,p2,k+,q2,);
  14. cout<<mid[k];
  15. }
  16.  
  17. int main()
  18. {
  19. while(cin>>pre>>mid)
  20. {
  21. int l=strlen(pre)-;
  22. T(,l,,l,);
  23. cout<<endl;
  24. }
  25. return ;
  26. }

心得:
二叉树问题看起来很容易,但要对先序、中序、以及后序有了解,才能更好的做二叉树的题目。二叉树问题无非就是两序找一序,一般都是用递归方法来实现,要好好学好递归,递归应用的方面有很多。

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. Exponentiation

    Description Problems involving the computation of exact values of very large magnitude and precision ...

  2. poj 3764 The xor-longest Path Trie

    题目链接 求树上的一条最长异或路径. 定义f(u, v)为u到v的路径, 那么显然f(1, u)^f(1, v) = f(u, v), 想不到这个就没有办法做. 然后就可以用字典树查询+插入了. 用指 ...

  3. 帝国cms7.0判断字段为空,显示为不同

    在一些情况下,我们的字段会留空,但是又不希望内容模板上只显示空.比如,在后台,如添加作者为小明,则显示小明,不填作者则显示"佚名",我们可以用以下代码. <? if($nav ...

  4. linux 查看文件系统类型

    1. mount [op@TIM ~]$ mount/dev/mapper/vg_tim-lv_root on / type ext4 (rw)proc on /proc type proc (rw) ...

  5. 异常Crash之 NSGenericException,NSArray was mutated while being enumerated

    *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NS ...

  6. Android 带password输入界面的Dialog实现机制

    1.布局实现: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...

  7. 【Android病毒分析报告】 - ZxtdPay 吸费恶魔

    本文章由Jack_Jia编写,转载请注明出处.  文章链接:http://blog.csdn.net/jiazhijun/article/details/11581543 作者:Jack_Jia    ...

  8. rman 使用catalog备份的演示

    介绍了如何使用catalog方式做RMAN备份,以及如何取消以catalog方式做备份. 第一步:创建RMAN CATALOG表空间及用户. [oracle@oel-01 ~]$ sqlplus / ...

  9. Objective-c Category(类别)

    category是Objective-c里面最常用的功能之一. category可以为已经存在的类增加方法,而不需要增加一个子类. 类别接口的标准语法格式如下: #import "类名.h& ...

  10. 【Linux操作系统分析】设备驱动处理流程

    1 驱动程序,操作系统,文件系统和应用程序之间的关系 字符设备和块设备映射到操作系统中的文件系统,由文件系统向上提供给应用程序统一的接口用以访问设备. Linux把设备视为文件,称为设备文件,通过对设 ...