【二叉树】hdu 1710 Binary Tree Traversals
acm.hdu.edu.cn/showproblem.php?pid=1710
【题意】
给定一棵二叉树的前序遍历和中序遍历,输出后序遍历
【思路】
根据前序遍历和中序遍历递归建树,再后续遍历输出
malloc申请空间在堆,函数返回,内存不释放,需要free手动释放
【Accepted】
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm> using namespace std;
const int maxn=1e3+;
int n;
int pre[maxn],in[maxn],post[maxn];
int mp[maxn];
struct node{
int id;
node *lef;
node *rig;
node(int i, node *l=NULL, node *r=NULL):id(i),lef(l),rig(r){}
};
node* dfs(int l,int r,int x,int y){
if(l>r) return NULL;
node *nd=(node *)malloc(sizeof(node));
nd->id=in[y];
nd->lef=dfs(l,y-,x+,mp[pre[x+]]);
nd->rig=dfs(y+,r,x+y-l+,mp[pre[x+y-l+]]);
return nd;
}
void postorder(node *root){
if(root==NULL) return;
postorder(root->lef);
postorder(root->rig);
if(root->id==pre[]){
printf("%d\n",root->id);
}else{
printf("%d ",root->id);
}
free(root);
}
int main(){
while(~scanf("%d",&n)){
for(int i=;i<n;i++){
scanf("%d",&pre[i]);
}
for(int i=;i<n;i++){
scanf("%d",&in[i]);
mp[in[i]]=i;
}
if(n==) {
printf("1\n");
continue;
}
node *root=dfs(,n-,,mp[pre[]]);
postorder(root);
}
return ;
}
【二叉树】hdu 1710 Binary Tree Traversals的更多相关文章
- hdu 1710 Binary Tree Traversals 前序遍历和中序推后序
题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...
- 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(树的建立,前序中序后序)
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 1710 Binary Tree Traversals(二叉树遍历)
传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...
- HDU 1710 Binary Tree Traversals
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗 ...
- 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 ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- java字符串拼接技巧(StringBuilder使用技巧)
在平时的开发中,我们可能会遇到需要拼接如下格式的字符串(至少我是遇到了很多次): 1,2,3,4,5,6,7,8,9,10,11,12,12,12,12,34,234,2134,1234,1324,1 ...
- 分享一个WebGL开发的网站-用JavaScript + WebGL开发3D模型
这张图每位程序员应该都深有感触. 人民心目中的程序员是这样的:坐在电脑面前噼里啪啦敲着键盘,运键如飞. 现实中程序员是这样的:编码5分钟,调试两小时. 今天我要给大家分享一个用WebGL开发的网站,感 ...
- 国家气象局提供的天气预报接口(完整Json接口)
国家气象局提供的天气预报接口主要有三个,分别是:http://www.weather.com.cn/data/sk/101010100.htmlhttp://www.weather.com.cn/da ...
- Robot Framework(十二) 执行测试用例——配置执行
3.4配置执行 本节介绍可用于配置测试执行或后处理输出的不同命令行选项.与生成的输出文件相关的选项将在下一节中讨论. 3.4.1选择测试用例 通过测试套件和测试用例名称 按标签名称 当没有测试匹配选择 ...
- BestCoder Round#15 1002-Instruction
http://acm.hdu.edu.cn/showproblem.php?pid=5083 官方题解——> 1002 Instruction 先考虑编码,首先找到operation对应的编码, ...
- myeclipse 导入项目时no projects are found to import解决办法
myeclipse 识别一个工程需要.classpath与.project文件,一般无需提交SVN所以项目切下来的时候是没有这两个文件的. 方法1: 1) 在myeclipse中新建一个和你要导入的项 ...
- css3中animation属性animation-timing-function知识点以及其属性值steps()
在animation中最重要的其实就是时间函数(animation-timing-function)这个属性,他决定了你的动画将以什么样的速度执行,所以最关键的属性值也就是cubic-bezier(n ...
- shell脚本,锁机制
[root@localhost wyb]# cat suijizhi.sh #!/bin/bash a=`|grep -v grep |wc -l` echo "$a" [ $a ...
- shell脚本,编写1个弹出式菜单的shell程序并实现其简单的菜单功能。
[root@localhost wyb]# cat zonghe.sh #!/bin/bash #zonghe usage(){ case $choice in ) read -p "ple ...
- javase(4)_数组
一.数组概述 数组可以看成是多个相同类型数据组合,对这些数据的统一管理. 数组变量属于引用类型,数组也可以看成对象,数组中的每个元素相当于该对象的成员变量. 数组中的元素可以是任意类型,包括基本类型和 ...