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数据库——迁移

    1.简介 迁移就像数据库的版本控制,允许团队简单轻松的编辑并共享应用的数据库表结构,迁移通常和Laravel的结构构建器结对从而可以很容易地构建应用的数据库表结构. Laravel的Schema门面提 ...

  2. Windows 10 优化

    ---恢复内容开始--- 0x00 使开始菜单,任务栏,和操作中心透明 --关闭 右下角开始菜单,选择设置,打开个性化菜单,找到颜色一栏.向下滑至最低端,使开始菜单,任务栏,和操作中心透明选项关闭 0 ...

  3. 让Category支持添加属性与成员变量【转载】

    Category是Objective-C中常用的语法特性,通过它可以很方便的为已有的类来添加函数.但是Category不允许为已有的类添加新的属性或者成员变量.     一种常见的办法是通过runti ...

  4. 跳转 nginx 跳转 apache跳转

    公司在google上投广告,需要做一些很简单的站去google上投广告,当用户在google上点击那些很简单的网站的时候,就会跳转到真实的网站.但是,如果用户直接在浏览器输入域名,并访问的话,那样就不 ...

  5. php跨域共享session

    . $gb_DBHOSTname = "127.0.0.1"; //主机的名称或是IP地址 02 $gb_DBname = "dbname"; //数据库名称 ...

  6. java中的多线程高并发与负载均衡的用途

    感觉对于这两问题的描述,大家很迷惑把 .下面我就介绍一下: 一; 什么是java的高并发,在什么情况下产生的? 答:如果网站的访问量非常大的话,我们就应该考虑高并发的情况. 高并发的时候就是有很多用户 ...

  7. Python3 多线程 学习 threading

    #-*- coding:utf-8 --*- #多线程测试 import time import datetime import threading def worker(): print(" ...

  8. CSU 1663: Tree(树链剖分)

    1663: Tree Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 26  Solved: 11 [Submit][id=1663"> ...

  9. easyui combobox 三级级联 input 两种实现

    /**<img src='${pageContext.request.contextPath}/images/block.png'/> * @param 默认载入 省市 */ $(func ...

  10. 创建有提示的ui组件

    using UnityEditor; using UnityEngine; using System.Collections; using Edelweiss.CloudSystem; namespa ...