/*
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. Mac上好用软件集锦

    1. 代码编辑器 TextMate 开源免费 2. 图像处理工具  Seahorse 开源免费 3. 远程Windows桌面连接工具 CoRD 开源免费 4. 办公软件 LibreOffice 开源免 ...

  2. spring jdbcTemplate query

    1. spring jdbcTemplate query需要实现mapRow方法 package com.cdv.apolloagent.jdbc.dao.impl; import java.sql. ...

  3. 数据库设计==>>MySchool

    1.数据库设计的步骤 第一步:需求分析(收集信息) 第二步:绘制 E-R 图 (标示实体 ,找到实体的属性 第三步:将 E-R 图转换成数据库模型图 第四步:将数据库模型图转换成数据表 2.如何绘制 ...

  4. android studio :com.android.support:appcompat-v7:21.+ 报错

    android studio :com.android.support:appcompat-v7:21.+ 报错: 在project——>app——>build.gradle修改: app ...

  5. Ansible用于网络设备管理 part 3 使用NAPALM成品库

    闲话 经过了这俩月的闲暇时间的瞎逛和瞎琢磨,我发现NAPALM是一条路,NAPALM是由帅哥David Barroso和美女Elisa Jasinska创建的一个项目,都是颜值高的技术牛人啊,真是不给 ...

  6. chenxi的html学习笔记

    0.本文主体源自:http://www.cnblogs.com/coco1s/p/4034937.html,有兴趣的可以直接去那里看,也可以看看我整理加拓展的.1.浏览器内核: 1.ie:triden ...

  7. 管理系统的前端解决方案:Pagurian V1.3发布

    Pagurian 一个管理系统的前端解决方案, 致力于让前端设计,开发,测试,发布更简单. 功能简介 Pagurian 适用于Web管理级的项目 基于Sea.js遵循CMD规范,友好的模块定义,使业务 ...

  8. 0728关于html的几个基础知识点

    1.文本的格式化. 文本的格式包括粗体,斜体等.<b></b>定义粗体,<i>定义斜体</i>,<del></del> 定义删除 ...

  9. SharePoint 2013: Search Architecture in SPC202

    http://social.technet.microsoft.com/wiki/contents/articles/15989.sharepoint-2013-search-architecture ...

  10. 【原】兼容IOS6以及旧版本的旋转处理方法,心得总结

    最近的项目需要频繁处理屏幕的旋转以及各控件的自适应坐标.IOS6出来之后,屏幕旋转的处理方法变得复杂很多.在查阅了很多资料以及动手测试之后,得出以下几点精简的体会: 对于IOS6.0以上版本: 1.如 ...