HDU 1710 Binary Tree Traversals
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历
首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树
前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗树的根节点,
然后拆分出左右子树,对左右子树进行相同的操作,也就是将建树的这个函数递归调用下去
build函数还是理解了好久啊话说= =仍然是学习的代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<algorithm>
using namespace std; typedef long long LL;
const int maxn=; void build(int n,int *s1,int *s2,int *s){
if(n<=) return;
int p;
for(int i=;i<n;i++){
if(s2[i]==s1[]){
p=i;//在中序遍历中找到根结点的位置
break;
}
} build(p,s1+,s2,s);//p为左子树的区间长度,s1+1是在前序遍历中左子树的开头,s2是在中序遍历中左子树的开头
build(n-p-,s1+p+,s2+p+,s+p);//n-p-1为右子树的区间长度,s1+p+1 是在前序遍历中右子树的开头,s2是在中序遍历中右子树的开头
s[n-]=s1[];
} int main()
{
int n,i,ans[maxn],s1[maxn],s2[maxn];
while(scanf("%d",&n)!=EOF){
for(i=;i<n;i++) scanf("%d",&s1[i]);
for(i=;i<n;i++) scanf("%d",&s2[i]);
build(n,s1,s2,ans);
for(i=;i<n-;i++) printf("%d ",ans[i]);
printf("%d\n",ans[i]);
}
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
acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 m ...
- hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)
Binary Tree Traversals T ...
- HDU 1710 二叉树的遍历 Binary Tree Traversals
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- Discuz!NT3.6与网站整合(操作用户信息)解决方案
因为网站要加个论坛,所以就用到了Discuz!NT3.6. 可惜目前官方论坛已经关闭,只有3.6版本的有源码,3.9的没有源码,不好操作,下载地址: http://download.comsenz.c ...
- 通过email分享
MFMailComposeViewController *mailC=[[MFMailComposeViewControlleralloc] init]; if ([MFMailComposeView ...
- Codeforces Gym 100342J Problem J. Triatrip 三元环
题目链接: http://codeforces.com/gym/100342 题意: 求三元环的个数 题解: 用bitset分别统计每个点的出度的边和入度的边. 枚举每一条边(a,b),计算以b为出度 ...
- idea从vcs引入maven项目报错
一.问题 用idea从cvs上check out的maven项目,打开后,发现依赖的jar包都有红色下划线.检查本地的maven库中有对应的包,那就是依赖有问题,idea没有在本地找到对应的包. 二. ...
- hibernate Session
转: http://kayo.iteye.com/blog/204143 Session 接口 Session 接口对于Hibernate 开发人员来说是一个最重要的接口.然而在Hibernate 中 ...
- oracle 字符集导入、导出 、转换
导入导出及转换 导入导出是我们常用的一个数据迁移及转化工具,因其导出文件具有平台无关性,所以在跨平台迁移中,最为常用. 在导出操作时,非常重要的是客户端的字符集设置,也就是客户端的NLS_LANG设置 ...
- spring测试父类,使用junit-4.4.jar,spring-test.jar
@ContextConfiguration(locations = "classpath:conf/applicationContext.xml") @RunWith(Spring ...
- POJ1068Parencodings
http://poj.org/problem?id=1068 这个题的话就是先把给出来的一串数字转化成括号,再把括号转化成要求的,最后输出就行了 #include<cstdio> #inc ...
- Eclipse 编译错误 Access restriction:The type *** is not accessible due to restriction on... 解决方案
报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C ...
- P1082 找朋友
描述 童年的我们,对各种事物充满了好奇与向往.这天,小朋友们对数字产生了兴趣,并且想和数字交朋友.可是,怎么分配这些数字才能使得每个小朋友都唯一地找到一个数字朋友呢?C小朋友说:咱们按自己名字的字典序 ...