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. laravel处理ajax的post提交

    Html页面(laravel表单提交必须token)所以 头部要加入: <meta name="csrf-token" content="{{ csrf_token ...

  2. explicit 和 implicit 的用法

    explicit 和 implicit 属于转换运算符,如用这两者可以让我们自定义的类型支持相互交换 explicti 表示显式转换,如从 A -> B 必须进行强制类型转换(B = (B)A) ...

  3. python单元测试unittest实例详解

    转自:http://blog.csdn.net/five3/article/details/7104466 单元测试作为任何语言的开发者都应该是必要的,因为时隔数月后再回来调试自己的复杂程序时,其实也 ...

  4. 在freemarker文件中,html标签获取后台的值

    1.<#assign a='3333' /> 2.<input type="text" id="name" name="name&q ...

  5. 隐藏VS2013的反馈、通知和登录按钮

    Visual Studio 2013的右上角有反馈.通知.登录.快速启动等按钮,在VS2013中没有选项可以设置为隐藏. 打开注册表(开始 -> 运行 -> regedit),展开到以下路 ...

  6. Cocoa 静态显示一个对话框

    M // // form2.m // test_multi_window // // Created by on 23/7/14. // Copyright (c) 2014 EDU. All rig ...

  7. k8s部署dns

    硬件环境: 两台虚拟机, 10.10.20.203 部署docker.etcd.flannel.kube-apiserver.kube-controller-manager.kube-schedule ...

  8. PHP-Socket-阻塞与非阻塞,同步与异步概念的理解

    1. 概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式:同步:      所谓同步,就是在发出一个功能调用时,在没 ...

  9. saltstack之文件管理

    1.managed文件管理 /srv/salt/file/managed.sls /tmp/hyxc: file.managed: - source: - salt://files/hyxc - sa ...

  10. 微信小程序的文件结构 —— 微信小程序教程系列(1)

    所有文章均是CSDN博客所看,已按照作者要求,注明出处了,感谢作者的整理! 博客文章地址:http://blog.csdn.net/michael_ouyang/article/details/548 ...