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
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗 ...
随机推荐
- linux下的3种DDOS软件介绍
什么是TFN2K? TFN2K 的作者是著名的德国黑客mixter. http://mixter.void.ru/papers.html TFN2K通过主控端利用大量代理端主机的资源进行对一个或多个目 ...
- 使用Firebug进行断点调试详解
利用Firebug我们可以非常方便地对网页上的任何JavaScript代码进行断点调试. 首先,使用快捷键F12在当前页面打开Firebug,并切换到脚本选项卡. 其次,我们需要为指定的js代码添加断 ...
- java多线程编码注意事项
Sole purpose of using concurrency is to produce scalable and faster program. But always remember, sp ...
- Java编程之路相关书籍(三个维度)
一.关于Java的技术学习.能够依照以下分三个维度进行学习 : (1)向下发展,也就是底层的方向 建议看<深入Java虚拟机>.<Java虚拟机规范>.<Thinking ...
- 出现windows启动服务失败(无法从命令行或调试器启动,需要安装InstallUtil.exe)的解决办法
两种方法1 从命令行安装2 选择项目-视图-自定义操作 然后将 安装,提交,回滚,卸载分别加入自定义操作,注意InstallClass属性为true
- Android Studio 默认的快捷键
参考资料: 1.http://stormzhang.com/devtools/2014/12/09/android-studio-tutorial3/ Action Mac OSX Win/Linux ...
- java 十进制转十六进制、十进制转二进制、二进制转十进制、二进制转十六进制
//10进制转16进制 Integer.toHexString(20); //10进制转2进制 Integer.toBinaryString(10); //16进制转10进制 Integer.pars ...
- eclipse导入web工程变成Java工程,解决方案
经常在eclipse中导入web项目时,出现转不了项目类型的问题,导入后就是一个java项目. 解决步骤: 1.进入项目目录,可看到.project文件,文本编辑器打开. 2.找到<nature ...
- JAVA中sleep() 和 wait() 有什么差别?
(网上的答案:sleep是线程类(Thread)的方法,导致此线程暂停运行指定时间,将运行机会给其它线程.可是监控状态依旧保持,到时后会自己主动恢复.调用sleep不会释放对象锁. wait是Obje ...
- lua面向对象铺垫
Account = { balance = , withdraw = function(self, v) self.balance = self.balance - v end } --:操作符隐藏了 ...