传送门

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<bits/stdc++.h>
using namespace std;
const int maxn = 10005;

void post(int n,int a[],int b[],int c[])
{
    if (n <= 0)    return;
    int pos;
    for (int i = 0;i < n;i++)
        if (b[i] == a[0])    pos = i;
    post(pos,a + 1,b,c);
    post(n - pos - 1,a + pos + 1,b + pos + 1,c + pos);
    c[n - 1] = a[0];
}

int main()
{
    int N;
    while (~scanf("%d",&N))
    {
        int a[maxn],b[maxn],c[maxn];
        for (int i = 0;i < N;i++)    scanf("%d",&a[i]);
        for (int i = 0;i < N;i++)    scanf("%d",&b[i]);
        post(N,a,b,c);
        printf("%d",c[0]);
        for (int i = 1;i < N;i++)    printf(" %d",c[i]);
        printf("\n");
    }
    return 0;
}
 
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
typedef struct Tree{
	Tree *left,*right;
	int val;
}Tree;
Tree *head;
Tree *build(int a[],int b[],int N)
{
	Tree *node;
	for (int i = 0;i < N;i++)
	{
		if (b[i] == a[0])
		{
			node = (Tree *)malloc(sizeof(Tree));
			node->val = a[0];
			node->left = build(a + 1,b,i);
			node->right = build(a + i + 1,b + i + 1, N - i - 1);
			return node;
		}
	}
	return NULL;
}

void Print(Tree *p)
{
	if (p == NULL)	return;
	Print(p->left);
	Print(p->right);
	if (p == head)	printf("%d\n",p->val);
	else	printf("%d ",p->val);
	free(p);
}

int main()
{
	int N,a[maxn],b[maxn];
	while (~scanf("%d",&N))
	{
		for (int i = 0;i < N;i++)	scanf("%d",&a[i]);
		for (int i = 0;i < N;i++)	scanf("%d",&b[i]);
		head = build(a,b,N);
		Print(head);
	}
	return 0;
}

  

HDU 1710 Binary Tree Traversals(二叉树遍历)的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 1710 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

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

  7. HDU 1710 Binary Tree Traversals

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

  8. hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)

                                                                                Binary Tree Traversals T ...

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

随机推荐

  1. 面向对象的PHP

    类的实例(包括继承) <?php // 父类 class Animal { public $name; public $age; // 构造函数,使用new操作符生成实例的时候自动调用 func ...

  2. 你真的理解 new 了吗?

    开篇先提几个问吧,如果你对这些问题都清楚了,那说明对于 new  这个关键字已经掌握得很好了,也不再需要花时间来阅读本文了, 1   new  一个class  与 new   一个Struct有什么 ...

  3. 系统升级日记(4):如何快速的修改Infopath中的各种URL

    摘要: 最近一段时间在公司忙于将各类系统进行升级,其最主要的目标有两个,一个是将TFS2010升级到TFS2013,另外一个是将SharePoint 2010升级到SharePoint 2013.本记 ...

  4. bootstrap点滴

    1.nav-stacked 这个属性可以决定 tab的变为竖的,不添加的话为横向的. 2.tab  横向的 ul中必须含有nav nav-tabs ul li a 中必须有data-toggle=&q ...

  5. Exif

    Exif是一种图像文件格式,它的数据存储与JPEG格式是完全相同的.实际上Exif格式就是在JPEG格式头部插入了数码照片的信息,包括拍摄时的光圈.快门.白平衡.ISO.焦距.日期时间等各种和拍摄条件 ...

  6. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  7. Hibernate之Annotation(注解的方式,非映射)

    在hibernate 3.0之后,可以建立一个符合JPA标准的Annotation,以hibernate3.3.2GA为例 Annotation 以 hibernate Annotation 3.3. ...

  8. kill 根据PID终止进程

    根据PID终止进程 kill [option] PID-list kill 通过向一个或多个进程发送信号来终止进程.除超级用户外,只有进程的所有者才可以对进程执行kill 参数 PID-list为ki ...

  9. mysql性能优化-慢查询分析、优化索引和配置

    一.优化概述 二.查询与索引优化分析 1性能瓶颈定位 Show命令 慢查询日志 explain分析查询 profiling分析查询 2索引及查询优化 三.配置优化 1)      max_connec ...

  10. 1104关于优化mysql服务器几个参数和思路

    转自http://www.cnblogs.com/AloneSword/p/3207697.html 按照从大到小,从主要到次要的形式,分析 mysql 性能优化点,达到最终优化的效果. 利用 min ...