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树,说明中 ...
随机推荐
- WinForm给控件加入hint文字
本文代码主要是参考别人的,仅为个人记录,方面后续使用~ 效果图: 主要代码在一个Win32Utility类中,代码如下: public static class Win32Utility { [Dll ...
- sql server:compare data from two tables
--Comparing data between two tables in SQL Server --Create two Tables-- CREATE TABLE TableA(ID Int, ...
- python爬虫——爬取NUS-WIDE数据库图片
实验室需要NUS-WIDE数据库中的原图,数据集的地址为http://lms.comp.nus.edu.sg/research/NUS-WIDE.htm 由于这个数据只给了每个图片的URL,所以需 ...
- 获取用户的真实ip
常见的坑有两个: 一.获取的是内网的ip地址.在nginx作为反向代理层的架构中,转发请求到php,java等应用容器上.结果php获取的是nginx代理服务器的ip,表现为一个内网的地址.php获取 ...
- 关于网络上的各种mysql性能测试结论
关于网上的各种性能测试帖子,我想说以下几点: 1.为了使性能测试更加的客观.实际,应该说明针对什么场景进行测试,查询.还是修改,是否包含了主键,包含了几个索引,各自的差别是什么.因为不同的mysql分 ...
- ECMAScript 6学习笔记(一):展开运算符
同步发布于:https://mingjiezhang.github.io/(转载请说明此出处). JavaScript是ECMAScript的实现和扩展,ES6标准的制定也为JavaScript加入了 ...
- sap透明表、结构、簇介绍以及查找表方法
sap透明表.结构.簇介绍以及查找表方法 一些人在写开发功能说明书的时候不知道如何去找屏幕字段对应的透明表,下面我来介绍一个比较有效的方法:首先简单介绍一下概念:在SAP中的表的种类有以下三种:Tra ...
- Web打印控件
Lodop是什么? 有人说她是Web打印控件,因为她能打印.在浏览器中以插件的形式出现,用简单一行语句就把整个网页打印出来: 有人说她是打印编程接口,因为她介于浏览器和打印设备之间,是个通道和桥梁,几 ...
- SharePoint 中用户控件的开发及应用
1.新建解决方案以及SharePoint项目,步骤比较简单略过,然后映射CONTROLTEMPLATES文件夹,在里面添加用户控件(仅场解决方案),如下图: 2.解决方案结构,如下图: 简单介绍一下, ...
- MVC中的CSRF解决方案
我们使用Ajax访问请求的时候,攻击者可能盗用了用户身份,以用户合法身份发送恶意请求. 具体预防措施, 1.在Html表单里面使用@Html.AntiForgeryToken(),这玩意会生成一对加密 ...