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

  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. D - 二叉树遍历(推荐)

    二叉树遍历问题 Description   Tree Recovery Little Valentine liked playing with binary trees very much. Her ...

  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. WCF—Binding

    原文地址:http://www.cnblogs.com/jams742003/archive/2010/01/13/1646379.html Binding描述了哪些层面的信息 一个Binding包含 ...

  2. [转]C服务端与java客户端的socket通信注意事项

    http://blog.csdn.net/gaoxin1076/article/details/7671752 Socket网络通讯开发总结之:Java 与 C进行Socket通讯 注意以下问题: 1 ...

  3. Cookie介绍及JavaScript操作Cookie方法详解

    本文主要为大家简单介绍了以下Cookie的用途.运行机制,以及JavaScript操作Cookie的各种方法,总结的比较全面,希望能给大家带来帮助. 什么是 Cookie “cookie 是存储于访问 ...

  4. webstrom开发微信小程序说明

    在操作之前,需要对webstrom做一些设置,如下 如果未安装node.js的朋友,请到如下地址 https://nodejs.org/en/ 安装(相信大家都是会的),如果安装完了之后,就使用如下的 ...

  5. Android群英传》读书笔记 (1) 第一章 Android体系与系统架构 + 第二章 Android开发工具新接触

    第一章 Android体系与系统架构 1.Dalvik 和 ARTDalvik好比是一辆可折叠的自行车,平时是折叠的,只有骑的时候,才需要组装起来用.ART好比是一辆组装好了的自行车,装好就可以骑了. ...

  6. HTML5之兴趣爱好

  7. Oracle 存储过程(2)

    http://www.cnblogs.com/chinafine/archive/2010/07/12/1776102.html http://blog.itpub.net/29485627/view ...

  8. niop 2014寻找道路

    /* 乍一看就是个最短路 SFPA 但是要保证路径上的所有点的出边所指向的点都直接或间接与终点连通. 这一点就蛋疼了0.0 开始想的是正着跑一边 每一个点的所有边都能符合条件 那这个点就符合条件0.0 ...

  9. Break和Continue的一些注意事项

    Break: 1)可以用于switch-case判断中,用于跳出switch 2)用在循环中,用于立即跳出循环 其中,用于循环的情况下,跳出的是break所在的循环 Continue: 立即结束本次循 ...

  10. dedecms调用所有顶级栏目最新文章的实现方法

    做dedecms的模板,我们会遇到各种各样的调用问题,dedecms列表页调用所有顶级栏目文章列表的方法如下所述: 在文章页面经常使用的是 {dede:arclist orderby='pubdate ...