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树,说明中 ...
随机推荐
- MySQL联接操作
在MySQL中,联接是一种对表的引用, 多表联接类型: 1.笛卡尔积(交叉联接):在MySQL中为CROSS JOIN或省略JOIN,如: select * from course, teachcou ...
- fibonacci数列的和取余(1)
As we know , the Fibonacci numbers are defined as follows: """" Given two numbe ...
- mysql zip install
1.Question Description: 1.1 version: mysql-5.7.11-64 1.2 form: zip file 1.3 >mysqld --install (su ...
- C语言范例学习02
第二章 指针 算是重点吧,这也是C语言的特色啊,直接访问物理存储. 重点: 指针就是一个存放它指向变量地址的变量,好绕口. 区分*在定义是与引用是的作用. 区分*.&的不同. 指针 ...
- Linux命令详解之—cat命令
cat命令的功能是连接文件或标准输入并打印,今天就为大家介绍下Linux中的cat命令. 更多Linux命令详情请看:Linux命令速查手册 Linux 的cat命令通常用来显示文件内容,也可以用来将 ...
- mybatis 下划线转驼峰配置
一直以来,在sqlmap文件中,对于数据库中的下划线字段转驼峰,我们都是通过resultmap来做的,如下: <resultMap id="ISTableStatistics" ...
- ASP.NET MVC 5 学习教程:快速入门
起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 通过控制器访问模型的数据 生成的代码详解 使用 SQL Se ...
- Erlang进程间消息接收超时设定
Erlang消息接收函数,一般都会设计成尾递归调用自己的模式.但是这样的模式,如果没有消息则会无限的等待下去,所以为了不无限等待,这里可以加个超时设定,例如: flush() -> re ...
- JSP page指令详解
JSP指令用来设置整个JSP页面相关的属性,如网页的编码方式和脚本语言. 语法格式如下: <%@ directive attribute="value" %> 指令可以 ...
- iOS 秒数转换成时间,时,分,秒
//转换成时分秒 - (NSString *)timeFormatted:(int)totalSeconds{ int seconds = totalSeconds % 60; int min ...