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

原创 2015年07月06日 22:05:12

题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710

Binary Tree Traversals

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4205    Accepted Submission(s): 1904


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
 

题意:给出前序历遍和中序遍历的顺序,来推导后序遍历的顺序。
做法:把握两个要点,前序是 先根历遍的,所以前序的第一个是根。然后这个根把中序分为两半,左边是左子树,右边是右子树。然后递归下就ok了。


#include <cstdio>
#include <algorithm>
using namespace std;

struct tree
{
	struct tree* l;
	struct tree* r;
	int val; 
	tree()
	{
		l=NULL;
		r=NULL;
	}
}; 

int pre[1010],in[1010];
int n;
tree* root;
void build(int num,int l,int r,tree *rt)//pre num   in_l  in_r
{
	int flag=-1;
	while(flag==-1)
	{
		for(int i=l;i<=r;i++)
		{
			if(pre[num]==in[i])
				flag=i;
		}
		if(flag==-1)
			num++;
	}
	if(flag-l>0)
	{
		rt->l=(tree*)malloc(sizeof(tree));
		*(rt->l)=tree();
		build(num+1,l,flag-1,rt->l);
	}
	if(r-flag>0)
	{
		rt->r=(tree*)malloc(sizeof(tree));
		*(rt->r)=tree();
		build(num+1,flag+1,r,rt->r);
	}
	rt->val=pre[num];
}

void post(tree* nw)
{
	if(nw->l!=NULL)
	post(nw->l);
	if(nw->r!=NULL)
	post(nw->r);

	if(nw==root)
		printf("%d",nw->val);
	else
		printf("%d ",nw->val);

}

void del(tree* nw)
{
	if(nw->l!=NULL)
	del(nw->l);
	if(nw->r!=NULL)
	del(nw->r);
	free(nw);
}

int main() 
{
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
			scanf("%d",&pre[i]);
		for(int i=0;i<n;i++)
			scanf("%d",&in[i]); 
		root=(tree*)malloc(sizeof(tree));
		*root=tree();
		build(0,0,n-1,root); 
		post(root);
		puts(""); 
		del(root);  
	} 
	return 0;
} 




版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu1710 Binary Tree Traversals ----- 二叉树前序中序推后序

原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=1710 一:原题内容 Problem Description A binary tree i...
  • LaoJiu_
  • LaoJiu_
  • 2016年05月28日 22:31
  • 587

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

hdoj 1710 Binary Tree Traversals 【二叉树由前序和中序求后序】

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

HDU 1710 Binary Tree Traversals(二叉树的后序遍历)

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

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

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

Binary Tree Traversals 已知先序 中序 求后序

Binary Tree Traversals 纯属个人理解 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/3276...

PAT 1020. Tree Traversals (25)(树的构造与遍历,通过后序中序输出层次遍历)

题目1020. Tree Traversals (25)时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN,...

HDU1710 Binary Tree Traversals【树的遍历】

题目地址 Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...

数据结构之二叉树的遍历 HDU1710 Binary Tree Traversals

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

hdu 1710 Binary Tree Traversals 二叉树的遍历

参考链接: http://www.cnblogs.com/jiangjing/archive/2013/01/14/2860163.html 根据前序与中序推后序 大题思路: void dfs() ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1710 Binary Tree Traversals 前序遍历和中序推后序
举报原因:
原因补充:

(最多只允许输入30个字)