Problem 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
 
 
 
 

注: 已知二叉树的前序和中序遍历, 可以唯一确定二叉树的后序遍历, 但如果知道前序和后序,求中序遍历是不可能实现的.

 

算法:

由前序遍历的第一个元素可确定左、右子树的根节点,参照中序遍历又可进一步确定子树的左、

右子树元素。如此递归地参照两个遍历序列,最终构造出二叉树。

由前序和中序结果求后序遍历结果

树的遍历:给你一棵树的先序遍历结果和中序遍历的结果,让你求以后序遍历输出用递归。

每次把两个数组分成三个部分,父节点,左子树,右子树,把父节点放到数组里边,重复此步骤直到重建一棵新树

,  这时,数组里元素刚好是后序遍历的顺序

关键点:

中序遍历的特点是先遍历左子树,接着根节点,然后遍历右子树。这样根节点就把左右子树隔开了。而前序遍历的特点是先访问根节点,从而实现前序遍历结果提供根节点信息,中序遍历提供左右子树信息,从而实现二叉树的重建

【注明】

先序的排列里第一个元素是根,再比较中序的排列里根所在的位置,则能确定左子树,右子树元素个数numleft,numright且在先序排列里,先是一个根,再是numleft个左子树的元素排列,最后是numright个右子树的元素排列。

该过程就是从inorder数组中找到一个根,然后从preorder数组的位置来确定改点到底是左儿子还是右儿子。如此一直循环下去知道一棵完整的数建立完成。

#include <stdio.h>
#include <stdlib.h> const int MAX = 1000 + 10;
int n,in[MAX],pre[MAX];
typedef struct BITree
{
int data,index;
BITree *Left,*Right;
}BiTree,*Tree; void DFS(Tree &root,int index)
{
if(root == NULL){
root = (Tree)malloc(sizeof(BiTree));
root->data = in[index];
root->index = index;
root->Left = NULL;
root->Right = NULL;
}else
{
if(index < root->index)
DFS(root->Left,index);
else
DFS(root->Right,index);
}
} void CreateTree(Tree &root)
{
int i,j,index;
root = (Tree)malloc(sizeof(BiTree));
for(i = 1;i <= n;i++)
if(in[i] == pre[1])
{
root->data = pre[1];
root->index = i;
root->Left = NULL;
root->Right = NULL;
break;
}
index = i;
for(i = 2;i <= n;i++)
for(j = 1;j <= n;j++)
if(in[j] == pre[i])
{
if(j < index)
DFS(root->Left,j);
else
DFS(root->Right,j);
break;
}
} void PostOrder(Tree root,int x)
{
if(root == NULL) return ;
PostOrder(root->Left,x+1);
PostOrder(root->Right,x+1);
if(x == 0)
printf("%d",root->data);
else
printf("%d ",root->data);
} int main()
{
int i;
while(scanf("%d",&n)!=EOF)
{
Tree root;
for(i = 1;i <= n;i++)
scanf("%d",&pre[i]);
for(i = 1;i <= n;i++)
scanf("%d",&in[i]);
CreateTree(root);
PostOrder(root,0);
printf("\n");
}
return 0;
}

 
#include <iostream>
#include <cstdio>
using namespace std; const int MAX = 1000 + 10;
typedef struct BITree
{
int data;
BITree *Left,*Right;
BITree()
{
Left = NULL;
Right = NULL;
}
}*BiTree;
int pre[MAX],in[MAX]; void BuildTree(BiTree &root,int len,int pst,int ped,int inst,int ined)
{
int i,left_len = 0;
if(len<=0)return; //递归终止的条件
root = new BITree;
root->data = pre[pst];
for(i = inst;i <= ined;i++)
if(in[i] == pre[pst])
{
left_len = i - inst;
break;
}
BuildTree(root->Left,left_len,pst+1,pst+left_len,inst,i-1);
BuildTree(root->Right,len-left_len-1,pst+left_len+1,ped,i+1,ined);
} void PostTravel(BITree *root)
{
if(root)
{
PostTravel(root->Left);
PostTravel(root->Right);
printf("%d ",root->data);
}
} int main()
{
int i,n;
BiTree root;
while(scanf("%d",&n)!=EOF)
{
for(i = 1;i <= n;i++)
scanf("%d",&pre[i]);
for(i = 1;i <= n;i++)
scanf("%d",&in[i]);
BuildTree(root,n,1,n,1,n);
PostTravel(root->Left);
PostTravel(root->Right);
printf("%d\n",root->data);
}
return 0;
}

Hdu Binary Tree Traversals的更多相关文章

  1. HDU 1710 二叉树的遍历 Binary Tree Traversals

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

  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(树的建立,前序中序后序)

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

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

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

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

                                                                                Binary Tree Traversals T ...

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

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

  7. HDU-1701 Binary Tree Traversals

    http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Tr ...

  8. HDU 1710-Binary Tree Traversals(二进制重建)

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

  9. Binary Tree Traversals(HDU1710)二叉树的简单应用

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

随机推荐

  1. CC EAL认证

    国际通用准则(CC) CC(Common Criteria)是国际标准化组织统一现有多种准则的结果,是目前最全面的评价准则.1996年6月,CC第一版发布:1998年5月,CC第二版发布:1999年 ...

  2. 垃圾回收算法简单介绍——JVM读书笔记&lt;二&gt;

    垃圾回收的过程主要包含两部分:找出已死去的对象.移除已死去的对象. 确定哪些对象存活有两种方式:引用计数算法.可达性分析算法. 方案一:引用计数算法 给对象中加入一个引用计数器.每当有一个地方引用它时 ...

  3. 海量数据解决思路之BitMap

    一.概述 本文将讲述Bit-Map算法的相关原理,Bit-Map算法的一些利用场景,例如BitMap解决海量数据寻找重复.判断个别元素是否在海量数据当中等问题.最后说说BitMap的特点已经在各个场景 ...

  4. oracle 命令创建用户 、授权、数据库导入、导出

    最近在使用oracle,经常要导入导出数据,命令很简单,却经常忘记,所以记下来.. drop user yfplss cascade;--登录system用户删除已存在的用户名,该用户下的所有东西都被 ...

  5. sql server存储过程分页

    Create PROCEDURE [dbo].[Table_GetList] ) = '', -- 查询条件(注意: 不要加 WHERE) ) = '', -- 设置排序 , -- 页尺寸 , -- ...

  6. HTML界面JQuery ajax 返回200,但走Error方法

    原因是JSON拼装的有问题. 都需要放在双引号里面,或者修改dataType的类型为  "html". http://blog.csdn.net/imjcoder/article/ ...

  7. JVM学习之对象的状态

    堆中存放着几乎所有的对象实例,垃圾收集器在堆堆进行回收前,首先要确定这些对象哪些还“活着”,哪些已经“死去”.方法有如下两种: (1)引用计数法 算法思想:为对象添加一个引用计数器,每当有一个地方引用 ...

  8. Highcharts使用手册

    chart: { type: 'area', ignoreHiddenSeries: false, //如果true,一旦一个系列被隐藏,轴将会扩展剩余的可见系列 }, 这是设置的两个纵坐标轴: yA ...

  9. python 学习笔记 9 -- Python强大的自省简析

    1. 什么是自省? 自省就是自我评价.自我反省.自我批评.自我调控和自我教育,是孔子提出的一种自我道德修养的方法.他说:“见贤思齐焉,见不贤而内自省也.”(<论语·里仁>)当然,我们今天不 ...

  10. hadoop笔记之Hive入门(Hive的体系结构)

    Hive入门(二) Hive入门(二) Hive的体系结构 ○ Hive的元数据 Hive将元数据存储在数据库中(metastore),支持mysql.derby.oracle等数据库,Hive默认是 ...