版权声明:本文为博主原创文章,未经博主同意不得转载。vasttian https://blog.csdn.net/u012860063/article/details/37699219

转载请注明出处:

viewmode=contents" rel="nofollow">http://blog.csdn.net/u012860063?viewmode=contents

题目链接:http://poj.org/problem?id=2255

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

The input 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

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 <cstdio>
#include <cstring>
int count;
char a[47],b[47]; void find(int start, int end)
{
int i;
if(start > end)
return;
char root = a[count++];
for(i = start; i <= end; i++)
{
if(b[i] == root)
{
break;
}
}
find(start,i-1);
find(i+1,end);
printf("%c",root);
}
int main()
{
while(scanf("%s%s",a,b)!=EOF)
{
count = 0;
int len = strlen(b);
find(0,len-1);
printf("\n");
}
return 0;
}

代码二:

#include <stdio.h>
#include <string.h>
#define N 27
char pre[N],in[N];
int id[N];
void print(int a,int b,int c,int d)
{
int i=id[pre[a]-'A'];
int j=i-c;
int k=d-i;
if(j) print(a+1,a+j,c,i-1);
if(k) print(a+j+1,b,i+1,d);
printf("%c",pre[a]);
}
int main()
{
while(~scanf("%s%s",pre,in))
{
for(int i=0;in[i];i++)
id[in[i]-'A']=i;
int len=strlen(in);
print(0,len-1,0,len-1);
puts("");
}
return 0;
}

poj 2255 Tree Recovery(求后序遍历,二叉树)的更多相关文章

  1. POJ 2255 Tree Recovery——二叉树的前序遍历、后序遍历、中序遍历规则(递归)

    1.前序遍历的规则:(根左右) (1)访问根节点 (2)前序遍历左子树 (3)前序遍历右子树 对于图中二叉树,前序遍历结果:ABDECF 2.中序遍历的规则:(左根右) (1)中序遍历左子树 (2)访 ...

  2. HDU1710---树(知前序遍历与中序遍历 求后序遍历)

    知前序遍历与中序遍历 求后序遍历 #include<iostream> #include<cstring> #include<queue> #include< ...

  3. codevs2010 求后序遍历

    难度等级:白银 2010 求后序遍历 题目描述 Description 输入一棵二叉树的先序和中序遍历序列,输出其后序遍历序列. 输入描述 Input Description 共两行,第一行一个字符串 ...

  4. YTU 2345: 后序遍历二叉树

    原文链接:https://www.dreamwings.cn/ytu2345/2611.html 2345: 后序遍历二叉树 时间限制: 1 Sec  内存限制: 128 MB 提交: 3  解决:  ...

  5. PHP递归方法实现前序、中序、后序遍历二叉树

    二叉树是每个节点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). class Node { public $value; pub ...

  6. php循环方法实现先序、中序、后序遍历二叉树

    二叉树是每个节点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). <?php class Node { public $v ...

  7. POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)

    链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...

  8. POJ 2255 Tree Recovery(根据前序遍历和中序遍历,输出后序遍历)

    题意:给出一颗二叉树的前序遍历和中序遍历的序列,让你输出后序遍历的序列. 思路:见代码,采用递归. #include <iostream> #include <stdio.h> ...

  9. POJ 2255 Tree Recovery 二叉树的遍历

    前序和中序输入二叉树,后序输出二叉树:核心思想只有一个,前序的每个根都把中序分成了两部分,例如 DBACEGF ABCDEFG D把中序遍历的结果分成了ABC和EFG两部分,实际上,这就是D这个根的左 ...

  10. HDU 1710Binary Tree Traversals(已知前序中序,求后序的二叉树遍历)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1710 解题思路:可以由先序和中序的性质得到 : 先序的第一个借点肯定是当前子树的根结点, 那么在 中序 ...

随机推荐

  1. 深入浅出解读 Java 虚拟机的差别测试技术

    本文分享基于字节码种子生成有效.可执行的字节码文件变种,并用于 JVM 实现的差别测试.本文特别提出用于修改字节码语法的classfuzz技术和修改字节码语义的classming技术.上述变种技术系统 ...

  2. GBDT 算法:原理篇

    本文由云+社区发表 GBDT 是常用的机器学习算法之一,因其出色的特征自动组合能力和高效的运算大受欢迎. 这里简单介绍一下 GBDT 算法的原理,后续再写一个实战篇. 1.决策树的分类 决策树分为两大 ...

  3. Spring Boot 2.x (二):How Hello World & 热部署

    本篇摘要 上一篇文章,我们构建了一个HelloWorld的程序,现在,我们来看一下这个程序,下面是我们这一节要分析的点. spring-boot-starter-boot @SpringBootApp ...

  4. DSAPI之摄像头追踪指定颜色物体

    Private CAM As New DSAPI.摄像头_avicap32 Private Clr As Color = Color.FromArgb(230, 50, 50) Private _Lo ...

  5. DSAPI多功能组件编程应用-网络相关(上)

    [DSAPI.DLL下载地址]  DSAPI多功能组件编程应用-网络相关,网络相关编程有很多很多,这里讲解一下封装在DSAPI中的网络相关的功能,这些都是本人简化到极点的功能了,可以在软件开发过程中节 ...

  6. 在win10系统开启linux子系统

    1. 2.重启计算机 3.在winstore下载和安装 ubuntu 4.查看当前win10子系统的linux版本 lsb_release -a 5.设置root账号密码, 在终端输入命令 sudo ...

  7. C# 在PDF中绘制动态图章

    我们知道,动态图章,因图章中的时间.日期可以动态的生成,因而具有较强的时效性.在本篇文章中将介绍通过C#编程在PDF中绘制动态图章的方法,该方法可自动获取当前系统登录用户名.日期及时间信息并生成图章. ...

  8. 常用开发环境搭建配置教程(OneStall)

    最近想要做一个小东西,用到了下面几个中间件或者环境: Java Tomcat Maven MongoDB ZooKeeper Node 并且恰好碰到腾讯云打折,云主机原价100多一个月,花了30块钱买 ...

  9. sql server2008数据库迁移的两种方案

    方案一 1,先将源服务器上的数据库文件打包(包括mdf和ldf文件),并且复制到目标服务器上. 2,解压,然后在目标服务器上附加数据库 总结:适合数据库巨大(50GB以上),需要快速迁移数据,并且移动 ...

  10. java开发环境配置——JDK

    虽然网上有很多类似的文章了,第一次搭的时候也是看的网上的文章,但为了做个记录,自己也写一下,记录一下. 首先是先安装JDK,JDK下载可以直接去官网下载,地址:http://www.oracle.co ...