传送门

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. 让python在hadoop上跑起来

    duang~好久没有更新博客啦,原因很简单,实习啦-好吧,我过来这边上班表示觉得自己简直弱爆了.第一周,配置环境:第二周,将数据可视化,包括学习了excel2013的一些高大上的技能,例如数据透视表和 ...

  2. js从0开始构思表情插件

    前言: 由于公司开发项目需要用到表情插件,在网上百度了好久,很多表情插件,都是需要引用好多js文件,也没有现成的demo可以使用,还有一些插件是引用好多图片,每一个表情都要重新请求一下.为了这样一个功 ...

  3. leetcode - 位运算题目汇总(上)

    最近在看位运算的知识,十分感叹于位运算的博大精深,正好leetcode有 Bit Manipulation 的专题,正好拿来练练手. Subsets 给出一个由不同的数字组成的数组,枚举它的子数组(子 ...

  4. Scala函数式编程进阶

    package com.dtspark.scala.basics /** * 函数式编程进阶: * 1,函数和变量一样作为Scala语言的一等公民,函数可以直接赋值给变量: * 2, 函数更长用的方式 ...

  5. Tyk API网关介绍及安装说明

    Tyk API网关介绍及安装说明 Tyk是一个开源的轻量级API网关程序. 什么是API网关 API网关是一个各类不同API的前置服务器.API网关封装了系统内部架构,对外提供统一服务.此外还可以实现 ...

  6. 以HTML为表现的日志记录组件

    日志搬家:http://www.loogn.net/blog/Article.aspx?id=21 关于日志记录,如果你不想随用随写,又不想用log4net的话,也许你可以了解一下这个! 我也是个很懒 ...

  7. MEF

    详情见链接:http://www.cnblogs.com/comsokey/p/MEF1.html#top

  8. 文本域的宽度和高度应该用cols和rows来控制,还是 用width和height来控制

    文本域宽度如果用cols来控制,缩放网页的时候文本域的宽度不会自动变化 用width来表示就会跟着网页缩放而缩放 看到下面一段文字: 对于内容至上的网页,在禁用CSS的情况下,HTML内容要做到易于阅 ...

  9. Win7 64bit下32bit的 ODBC 数据源问题

    win764位有数据源,但是如果我们在win7 64bit中使用32位的数据源的时候,我们就需要对其进行配置,很有趣的是,64为的数据源我们可以在控制面板——系统与安全——管理工具——数据源,进入可对 ...

  10. JNI系列——简便开发流程

    1.编写Java代码 2.选中工程目录--右键单击Android Tools--Add Native Support 3.输入要生成的库名 4.到工程目录中jni目录下对自动生成文件和.mk文件进行相 ...