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

题目链接

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

这道题大致意思是给你一颗二叉树的先序和中序遍历,要求你求出其后序遍历。

做这道题,首先我们需要知道什么是二叉树?二叉树(binary tree)是指树中节点的度不大于2的有序树,它是一种最简单且最重要的树。(来自百度百科)

其特点就是每个结点最多只能有两棵子树,且有左右之分。

后面需要知道二叉树的几种遍历方式:

图一             图一

一、先序遍历:即按照二叉树的父结点、左儿子、右儿子的顺序进行访问。如图一,访问所返回的结果就是EBADCGFIH,先序遍历第一个结点是根。其伪代码是:

void preorder(node *root){
	if(root!=NULL){
		post[k++]=root->value;
		preorder(root->l);
		preoreder(root->r);
	}
}

二、中序遍历:即按照左儿子、父结点、右儿子的顺序进行访问,如图一,访问返回的结果就是ABCDEFGHI,伪代码如下:

void inorder(node *root){
	if(root!=NULL){
		inoreder(root->l);
		post[k++]=root->value;
		inoreder(root->r);
	}
}

三、后序遍历:即按照左儿子、右儿子、父结点访问。如图一,访问返回结果就是ACDBFGHIE,后序遍历的最后一个结点是根。伪代码如下:

void postorder(node *root){
	if(root!=NULL){
		postorder(root->l);
		postorder(root->r);
		post[k++]=root->value;
	}
}

其中如果先序遍历+中序遍历知道或者中序遍历+后序遍历知道,我们就可以知道另外一个遍历,先序遍历+后序遍历不可得知中序遍历。

树的存储一般是用指针来实现的,指向左、右子结点。

struct node{
	int value;
	node *l,*r;
	node(int value=0,node *l=NULL,node *r=NULL):value(value),l(l),r(r){}
}; 

建树

void build(int l,int r,int& t,node* &root){
	int flag=-1;
	for(int i=l;i<=r;i++){
		if(in[i]==pre[t]){
			flag=i;break;
		}
	}	
	if(flag==-1)return ;
	root =new node(in[flag]);
	t++;
	if(flag>l)build(l,flag-1,t,root->l);
	if(flag<r)build(flag+1,r,t,root->r);
}

本题代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1010;
int pre[maxn],in[maxn],post[maxn];
int k;
struct node{
	int value;
	node *l,*r;
	node(int value=0,node *l=NULL,node *r=NULL):value(value),l(l),r(r){}
}; 
void build(int l,int r,int& t,node* &root){  //建树
	int flag=-1;
	for(int i=l;i<=r;i++){
		if(in[i]==pre[t]){
			flag=i;break;
		}
	}	
	if(flag==-1)return ;
	root =new node(in[flag]);
	t++;
	if(flag>l)build(l,flag-1,t,root->l);
	if(flag<r)build(flag+1,r,t,root->r);
}
void preo(node *root){  //前序
	if(root!=NULL){
		post[k++]=root->value;
		preo(root->l);
		preo(root->r);
	}
}
void ino(node *root){  //中序
	if(root!=NULL){
		ino(root->l);
		post[k++]=root->value;
		ino(root->r);
	}
}
void posto(node *root){  //后序
	if(root!=NULL){
		posto(root->l);
		posto(root->r);
		post[k++]=root->value;
	}
}
void remove_tree(node *root){   //删树
	if(root==NULL)return ;
	remove_tree(root->l);
	remove_tree(root->r);
	delete root;
}
int main(){
	int n;
	while(~scanf("%d",&n)){
		for(int i=1;i<=n;i++)scanf("%d",&pre[i]);
		for(int i=1;i<=n;i++)scanf("%d",&in[i]);
		node *root;
		int t=1;
		build(1,n,t,root);
		k=0;
		posto(root);
		for(int i=0;i<k;i++)printf("%d%c",post[i],i==k-1?'\n':' ');
		remove_tree(root);
	}
}

道阻且长
自己选的路 跪着也要走完

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值