hdu1710-Binary Tree Traversals (由二叉树的先序序列和中序序列求后序序列)
http://acm.hdu.edu.cn/showproblem.php?pid=1710
Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4210 Accepted Submission(s): 1908
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.
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
题解:意思很直白。给你二叉树的先序序列和中序序列,让你求后序序列。先利用先序序列和中序序列的特征递归建树,再后序遍历。
代码:
#include <fstream>
#include <iostream>
#include <cstdio> using namespace std; const int N=;
struct node{
int c;
struct node *lch,*rch;
};
int n,tn;
int pre[N],in[N]; void createTree(int* l,int* r,int i,int j,int e,int f,node** tree);
void postOrder(node *p); int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
node *tree;
while(~scanf("%d",&n)){
for(int i=;i<n;i++)
scanf("%d",&pre[i]);
for(int i=;i<n;i++)
scanf("%d",&in[i]);
createTree(pre,in,,n-,,n-,&tree);
tn=;
postOrder(tree);
}
return ;
}
void createTree(int* l,int* r,int i,int j,int e,int f,node** tree){//可以思考下这里为什么是**,可不可以用*?
int m;
(*tree)=new node;
(*tree)->c=l[i];
m=e;
while(r[m]!=l[i]) m++;
if(m==e) (*tree)->lch=NULL;
else createTree(l,r,i+,i+m-e,e,m-,&(*tree)->lch);
if(m==f) (*tree)->rch=NULL;
else createTree(l,r,i+m-e+,j,m+,f,&(*tree)->rch);
}
void postOrder(node *p){
if(p!=NULL){
postOrder(p->lch);
postOrder(p->rch);
printf("%d",p->c);
if(++tn<n) printf(" ");
else puts("");
delete p;
}
}
hdu1710-Binary Tree Traversals (由二叉树的先序序列和中序序列求后序序列)的更多相关文章
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals (二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)
Binary Tree Traversals T ...
- HDU 1710 Binary Tree Traversals(二叉树遍历)
传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...
- hdu1710 Binary Tree Traversals(二叉树的遍历)
A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjo ...
- hdu1710 Binary Tree Traversals
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1710 题意:给前序.中序求后序,多组 前序:根左右 中序:左右根 分析:因为前序(根左右)最先出现的总 ...
- Binary Tree Traversals(HDU1710)二叉树的简单应用
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 二叉树的遍历 Binary Tree Traversals
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals(二叉树)
题目地址:HDU 1710 已知二叉树先序和中序求后序. #include <stdio.h> #include <string.h> int a[1001], cnt; ty ...
- HDU-1701 Binary Tree Traversals
http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Tr ...
随机推荐
- iptables规则绑定在port而不是拦截在协议栈
版权声明:本文为博主原创,无版权.未经博主同意能够任意转载,无需注明出处,任意改动或保持可作为原创! https://blog.csdn.net/dog250/article/details/2417 ...
- Maven中配置生成单元测试报告配置
对junit单元测试的报告: 1. ------------------------------------------------------- 2. T E S T S 3. ------ ...
- Mysql中五级权限小结
mysql的权限控制主要是通过mysql库下的db,user,host,table_priv,column_priv表控制. 由于权限信息数据量比较小,所以mysql在启动时会将所有的权限消息加载到内 ...
- 关于CSS单位:rem vh vw vmin vmax
rem(root em) 如果你给body设置了font-size字体大小,那么body的任何子元素的1em就是等于body设置的font-size demo: body { font-size: ...
- RK3288 dts和dtsi介绍
Device Tree 是一种描述硬件的数据结构,它起源于 OpenFirmware(OF).在 Linux2.6 中,ARM 架构的板机硬件细节过多地被硬编码在 arch/arm/plat-xxx ...
- Ubuntu 14.04 配置安卓5.1编译环境
Ubuntu 14.04版本 电脑cpu必须是64位 硬盘分配大约100G的空间 1.ubuntu中更新源 $ sudo apt-get update 2.android5.1需要安装openjdk- ...
- 使用Octave分析GNU Radio的数据
Octave 是 GNU Radio 的最流行的分析工具,因此 GNU Radio 软件包也包含它自身的一组脚本用于读取和语法分析输出.本文介绍如何使用 Octave 分析 GNU Radio 产生的 ...
- elastic search6.2.2 实现用户搜索记录查询(去重、排序)
elastic search6.2.2 实现搜索记录查询 ,类似新浪微博这种,同样的搜索记录后面时间点的会覆盖前面的(主要思路:关键词去重,然后按时间排序) 先创建索引 //我的搜索 PUT my_s ...
- mysql存储引擎之innodb学习
innodb引擎特点1.支持事务:支持4个事务隔离级别,支持多版本读. 2.行级锁定(更新时一般是锁定当前行):通过索引实现,全表扫描仍然会是表锁,注意间隙 锁的影响 3.读写阻塞与事务隔离级别有关 ...
- Python学习总结之一 -- 基础篇
Python学习第一篇 一:写在前面 啊,最近我的新博客一直都没有更新学习内容了,只是最近一直都在忙着寻找实习机会(或许这只是一个借口,真实原因是我太懒惰了,改改改!).终于今天又投递了几个新的实习职 ...