/*
Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4644 Accepted Submission(s): 2113 Problem Description
A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2. In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder. In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder. In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r. Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence. Input
The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree. Output
For each test case print a single line specifying the corresponding postorder sequence. Sample Input 9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6 Sample Output 7 4 2 8 9 5 6 3 1 Source
HDU 2007-Spring Programming Contest Recommend
lcy | We have carefully selected several similar problems for you: 1711 1708 1707 1709 1509
*/
#include <iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
const int Nmax=;
int pre[Nmax];
int in[Nmax];
int post[Nmax];
struct BTree
{
int data;
BTree *l;
BTree *r;
}*p;
BTree* creatBT(int ls,int rs,int pos)
{
BTree *p = new BTree;
if(ls==rs)
return NULL;
for(int i=ls; i<rs; i++)
{
if(in[i]==pre[pos])
{
p->data=pre[pos];
p->l=creatBT(ls,i,pos+);
p->r=creatBT(i+,rs,pos++i-ls);
}
}
return p;
}
void postTra(BTree* pt)
{
if(pt==NULL)
return;
postTra(pt->l);
postTra(pt->r);
if(pt==p)
printf("%d\n",pt->data);
else
printf("%d ",pt->data);
}
int main()
{
int i,n;
while(scanf("%d",&n)!=EOF)
{
p=NULL;
for(i=; i<n; i++)
scanf("%d",&pre[i]);
for(i=; i<n; i++)
scanf("%d",&in[i]);
p=creatBT(,n,);
postTra(p);
}
return ;
}

hdu1710(二叉树的历遍)的更多相关文章

  1. Python 历遍目录

    Automate the Boring Stuff 学习笔记 01 使用 os 模块的 walk() 函数可以实现历遍目录的操作,该函数接收一个绝对路径字符串作为必选参数,返回三个参数: 当前目录—— ...

  2. hdu1217(spfa,存在环,但需要将环的元素历遍一次.....求乘积的最大)

    题意:有n个国家货币,给出m种两个国家之间的货币兑换率,求是否可以盈利....... 思路:其实就是看国家货币兑换间是否存在一个环,使得从v点出发时,dis[v]=1,经过环回到v点时,dis[v]& ...

  3. LINUX下用C语言历遍目录 C语言列出目录 dirent.h在C/C++中的使用

    LINUX下历遍目录的方法一般是这样的打开目录->读取->关闭目录相关函数是opendir -> readdir -> closedir #include <dirent ...

  4. C# 历遍对象属性

    今天有个网友问如何历遍对象的所有公共属性,并且生成XML.采用序列化方式的话比较简单,我写个手工解析的例子,这样能让初学者更加理解也比较灵活,记录一下吧或许会有人用到. 对象模型: public cl ...

  5. hdu1710 二叉树的遍历

    Problem Description 已知前序和中序 求后序 Input The input contains several test cases. The first line of each ...

  6. c# 类的历遍和历遍操作

    string id = Request.Form["id"]; string type = Request.Form["type"]; string info ...

  7. Binary Tree Traversals(HDU1710)二叉树的简单应用

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  8. Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树

    Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树 Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序 ...

  9. Leetcode:1008. 先序遍历构造二叉树

    Leetcode:1008. 先序遍历构造二叉树 Leetcode:1008. 先序遍历构造二叉树 思路 既然给了一个遍历结果让我们建树,那就是要需要前序中序建树咯~ 题目给的树是一颗BST树,说明中 ...

随机推荐

  1. 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件

    [源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...

  2. 与众不同 windows phone (39) - 8.0 联系人和日历

    [源码下载] 与众不同 windows phone (39) - 8.0 联系人和日历 作者:webabcd 介绍与众不同 windows phone 8.0 之 联系人和日历 自定义联系人存储的增删 ...

  3. http 响应码

    一.HTTP码应码响应码由三位十进制数字组成,它们出现在由HTTP服务器发送的响应的第一行. 响应码分五种类型,由它们的第一位数字表示:1.1xx:信息,请求收到,继续处理2.2xx:成功,行为被成功 ...

  4. linux tcp/ip编程和windows tcp/ip编程差别以及windows socket编程详解

    最近要涉及对接现有应用visual c++开发的tcp客户端,花时间了解了下windows下tcp开发和linux的差别,从开发的角度而言,最大的差别是头文件(早期为了推广尽可能兼容,后面越来越扩展, ...

  5. Telegram传奇:俄罗斯富豪、黑客高手、极权和阴谋…

    说了很久要写Telegram的故事,一直拖延没有写.在我拖延的这段时间里面,Telegarm继续快速增长,前几天,在旧金山的TechCrunch Disrupt活动上,创始人Durov说现在Teleg ...

  6. SQLServer处理行转列和列转行

    掌握SQL Server 行转列和列转行 1.列转行 数据经过计算加工后会直接生成前端图表需要的数据源,但是程序里又需要把该数据经过列转行写入中间表中,下次再查询该数据时直接从中间表查询数据. 1.1 ...

  7. AloneJs.albumBox() —— 相册对话框

    一.引用 <link href="https://cdn.suziyun.com/alonejs.min.css" rel="stylesheet" /& ...

  8. 【使用 DOM】使用 DOM 元素

    1. 使用元素对象 HTMLElement对象提供了一组属性,可以用它们来读取和修改被代表的数据.下表介绍了这些属性. 下面代码展示了如何使用表中所列的一些基本属性. <!DOCTYPE htm ...

  9. .NET破解之轻量万能自定义信息管理系统

    一般敢说万能的莫非真有两把刷子.今天来破解试试,看效果好用不. 下载:http://down.chinaz.com/soft/36780.htm 补丁: http://www.t00y.com/fil ...

  10. 软件快速开发平台 JEPF

    JEPF新一代软件快速开发平台(Java Elephant Platform)是一款优秀的平台产品,它本着灵活.快捷开发.高性能.高协作性.高稳定性.高可用性.人性化的操作体验为设计宗旨历经2年研发成 ...