Binary Tree Traversals

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

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

The
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

For each test case print a single line specifying the corresponding postorder sequence.
 

Sample Input

9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
 

Sample Output

7 4 2 8 9 5 6 3 1
 
不知道为什么往函数里串地址就运行不了,找了半天没找到错误!
 这是错误的代码:

#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)(二叉树前序中序推后序)的更多相关文章

  1. HDU 1710 Binary Tree Traversals(二叉树)

    题目地址:HDU 1710 已知二叉树先序和中序求后序. #include <stdio.h> #include <string.h> int a[1001], cnt; ty ...

  2. HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

    题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...

  4. HDU 1710 Binary Tree Traversals (二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. hdu1710(Binary Tree Traversals)(二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. HDU 1710 Binary Tree Traversals(二叉树遍历)

    传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...

  7. 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 ...

  8. 【二叉树】hdu 1710 Binary Tree Traversals

    acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 m ...

  9. HDU 1710 Binary Tree Traversals

    题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗 ...

随机推荐

  1. 记录:Android中StackOverflow的问题

    最近新作的项目上线,出现了一个让人抓狂的问题.在此记录一下! 现在的项目中,制作了一个界面非常复杂.整个结构是最外层一个Layout,封装了Menu键吊起的菜单,整个内容使用一个FrameLayout ...

  2. Google 商店

    Google 商店地址:https://store.google.com/

  3. java比较字符串长度

    1.一种:str1.length()>=str.length():这种只比较字符串中的计算字符的数量,不管是中文还是英文 2:另一种:str1.getBytes().length>=str ...

  4. Linux上部署Java应用+Python3环境搭建

    给了Linux的测试环境,目前需要install JDK, Tomcat,此处记录下小白的操作过程. 1. 查询Linux发行版本,包括内核信息 (1) Linux查询内核信息 $ uname -a ...

  5. nginx大量TIME_WAIT的解决办法 netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

    vi /etc/sysctl.conf net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse=1 #让TIME_WAIT状态可以重用,这样即使TIME_W ...

  6. find 多文件查找需要单引号

    [root@db01 local]# find  -name '*.com'|xargs egrep "qq"./tt.com:qq[root@db01 local]# find  ...

  7. python第三方库地址

    python第三方库的地址: requests: http://docs.python-requests.org/zh_CN/latest/user/quickstart.html beautifus ...

  8. C++ 错误积累

    错误一 VS2012错误:不能在成员函数  的类外部重新声明该函数 解决:检查函数的大括号匹配

  9. struts2一个实例中遇到的问题

    今天实现了一个登录功能的Struts2小程序. 期间遇到了许多问题,记忆犹新的是 (1)新版本的tomcat9和eclipse Neon Release (4.6.0) 发生了冲突,启动服务器的时候老 ...

  10. They're much closer in spirit to how our brains work than feedforward networks.

    http://neuralnetworksanddeeplearning.com/chap1.html Up to now, we've been discussing neural networks ...