hdu1710(二叉树的历遍)
/*
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(二叉树的历遍)的更多相关文章
- Python 历遍目录
Automate the Boring Stuff 学习笔记 01 使用 os 模块的 walk() 函数可以实现历遍目录的操作,该函数接收一个绝对路径字符串作为必选参数,返回三个参数: 当前目录—— ...
- hdu1217(spfa,存在环,但需要将环的元素历遍一次.....求乘积的最大)
题意:有n个国家货币,给出m种两个国家之间的货币兑换率,求是否可以盈利....... 思路:其实就是看国家货币兑换间是否存在一个环,使得从v点出发时,dis[v]=1,经过环回到v点时,dis[v]& ...
- LINUX下用C语言历遍目录 C语言列出目录 dirent.h在C/C++中的使用
LINUX下历遍目录的方法一般是这样的打开目录->读取->关闭目录相关函数是opendir -> readdir -> closedir #include <dirent ...
- C# 历遍对象属性
今天有个网友问如何历遍对象的所有公共属性,并且生成XML.采用序列化方式的话比较简单,我写个手工解析的例子,这样能让初学者更加理解也比较灵活,记录一下吧或许会有人用到. 对象模型: public cl ...
- hdu1710 二叉树的遍历
Problem Description 已知前序和中序 求后序 Input The input contains several test cases. The first line of each ...
- c# 类的历遍和历遍操作
string id = Request.Form["id"]; string type = Request.Form["type"]; string info ...
- Binary Tree Traversals(HDU1710)二叉树的简单应用
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树
Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树 Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序 ...
- Leetcode:1008. 先序遍历构造二叉树
Leetcode:1008. 先序遍历构造二叉树 Leetcode:1008. 先序遍历构造二叉树 思路 既然给了一个遍历结果让我们建树,那就是要需要前序中序建树咯~ 题目给的树是一颗BST树,说明中 ...
随机推荐
- 重新想象 Windows 8.1 Store Apps (86) - 系统 UI 的新特性: Theme, 窗口宽度大小可变, ApplicationView, DisplayInformation
[源码下载] 重新想象 Windows 8.1 Store Apps (86) - 系统 UI 的新特性: Theme, 窗口宽度大小可变, ApplicationView, DisplayInfor ...
- LeetCode130:Surrounded Regions
题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...
- [moka同学笔记]yii2.0 dropdownlist的简单使用(一)
1.controller控制中 $modelCountrytelCode = CountryTelCode::find()->orderBy('id DESC')->all(); $tel ...
- java中使用 正则 抓取邮箱
我们来抓取豆瓣网的邮箱吧!把这个页面的所有邮箱都抓取下来 如https://www.douban.com/group/topic/8845032/: 代码如下: package cn.zhangzon ...
- Android5.0新特性——图片和颜色(drawable)
图片和颜色 tint属性 tint属性一个颜色值,可以对图片做颜色渲染,我们可以给view的背景设置tint色值,给ImageView的图片设置tint色值,也可以给任意Drawable或者NineP ...
- http服务&ajax编程
http服务&ajax编程 1.服务器 前言:通俗的讲,能够提供某种服务的机器(计算机)称为服务器 1.1.服务器类型 按照不同的划分标准,服务可划分为以下类型: 按服务类型可分为:文件服务器 ...
- Javascript中的Label语句
在javascript中,我们可能很少会去用到 Label 语句,但是熟练的应用 Label 语句,尤其是在嵌套循环中熟练应用 break, continue 与 Label 可以精确的返回到你想要的 ...
- Sass学习之路(1)——Sass简介
Sass是CSS的一种预处理器语言,类似的语言还有Less,Stylus等. 那么什么是CSS预处理器? CSS 预处理器定义了一种新的语言,其基本思想是,用一种专门的编程语言,为 CSS 增加了一些 ...
- Access sql语句创建表及字段类型
创建一张空表: Sql="Create TABLE [表名]" 创建一张有字段的表: Sql="Create TABLE [表名]([字段名1] MEMO NOT NUL ...
- Microsoft Dynamics CRM 2013 --选项集的多选
由于从Microsoft Dynamics CRM 2011到Microsoft Dynamics CRM 2013,界面的风格发生了很大的变化 故原先在2011上开发的选项集多选在2013上面已经不 ...