hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
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
Sample Input
Sample Output
#include <stdio.h>
#include <string.h>
int n;
int a[1000], b[1000], c[1000]; void build( int len, int s1, int s2, int s )
{
int p;
int i;
if(len<=0)
return;
else
{
for(i=s2; i<s2+len; i++ )
{
if(b[i]==a[s1])
{
p = i;
break;
}
}
c[s+len-1] = a[s1]; build(p, s1+1, s2, s ); //左
build(len-p-1, s1+p+1, s2+p+1, s+p); //右 }
}
int main()
{
int i, j;
while(scanf("%d", &n)!=EOF)
{
for(i=0; i<n; i++)
{
scanf("%d", &a[i] );
}
for(i=0; i<n; i++)
{
scanf("%d", &b[i] );
}
build(n, 0, 0, 0); for(i=0; i<n; i++)
{
printf("*%d ", c[i] );
}
} return 0;
}
#include <stdio.h>
#include <string.h>
void build(int len, int *s1, int *s2, int *s)
{
int p;
int i;
if(len<=0)
return; else
{
for(i=0; i<len; i++)
{
if(s2[i]==s1[0])
{
p = i;
}
}
build(p, s1+1, s2, s);
build(len-p-1, s1+p+1, s2+p+1, s+p);
s[len-1] = s1[0];
}
}
int main()
{
int i; int s1[1000], s2[1000], s3[1000];
int n;
while(scanf("%d", &n)!=EOF)
{
for(i=0; i<n; i++)
{
scanf("%d", &s1[i] );
}
for(i=0; i<n; i++)
{
scanf("%d", &s2[i] );
}
build(n, s1, s2, s3 );
for(i=0; i<n; i++)
{
printf("%d%c", s3[i], i==n-1?'\n':' ' );
}
} return 0;
}
hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)的更多相关文章
- HDU 1710 Binary Tree Traversals(二叉树)
题目地址:HDU 1710 已知二叉树先序和中序求后序. #include <stdio.h> #include <string.h> int a[1001], cnt; ty ...
- 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 前序遍历和中序推后序
题链;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 ...
- 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(二叉树遍历)
传送门 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 ...
- 【二叉树】hdu 1710 Binary Tree Traversals
acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 m ...
- HDU 1710 Binary Tree Traversals
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗 ...
随机推荐
- 在Java中怎样高效的推断数组中是否包括某个元素
来自 http://www.hollischuang.com/archives/1269? 怎样检查一个数组(无序)是否包括一个特定的值?这是一个在Java中经经常使用到的并且非常实用的操作.同一时候 ...
- stage3D基础四----Stage3D和透视投影的使用(转)
原文地址: http://www.adobe.com/cn/devnet/flashplayer/articles/perspective-projection.html 引言 在本教程中,你将了解透 ...
- hdu 3172 Virtual Friends(并查集,字典树)
题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...
- 题目1 : Farthest Point
时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 Given a circle on a two-dimentional plane. Output the integral ...
- php 生成8位数唯一的激活码
/** *生成激活码 */ function showGenerationActivationCode(){ #渠道类型id $channel_id=$_POST['channel']; #根据渠道i ...
- TortoiseSVN与VisualSVN Server搭建SVN版本控制系统(转)
地址:http://www.cnblogs.com/xing901022/p/4399382.html
- iOS 9之3D Touch功能
首先要有真机iPhone 6s以上,开发工具Xcode 7,然后在官方文档拷贝一段文字就可以了. <key>UIApplicationShortcutItems</key> ...
- web安全之SQL注入---第二章 什么是sql注入?
如何理解SQL注入?SQL注入是一种将SQL代码添加到输入参数中传递到SQL服务器解析并执行的一种攻击方法总结:其实就是输入的参数没有进行过滤,直接参加sql语句的运算,达到不可预想的结果.SQL注入 ...
- openCV图像形态学
#include <cv.h> #include <highgui.h> #include <stdio.h> //平滑处理 int main() { IplIma ...
- Devexpress GridView 常用操作总结
一:Clone返回新的 DataTable Clone返回新的 DataTable,与当前的 DataTable 具有相同的架构:Copy:返回新的 DataTable,它具有与该 DataTable ...