Binary Tree Traversals hdu

本文探讨了二叉树的前序、中序和后序遍历算法,并提供了从前序和中序遍历序列推导出后序遍历序列的具体实现方法。通过两个示例代码,展示了如何使用递归构建二叉树并进行遍历,适用于算法学习和竞赛准备。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Binary Tree Traversals
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11643 Accepted Submission(s): 5186

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

Source
HDU 2007-Spring Programming Contest

#include<bits/stdc++.h>
using namespace std;
int pre[1010],mid[1010];
struct Tree{
	int data;
	Tree *left;
	Tree *right;
}*root;
Tree * create(int *a,int *b,int n)
{
	Tree *t;
	for(int i=0;i<n;i++){
		if(a[0]==b[i]){
			t=(Tree*)malloc(sizeof(Tree));
			t->data =b[i];
			t->left =create(a+1,b,i);
			t->right =create(a+i+1,b+i+1,n-i-1);
			return t;
		}
	}
	return NULL;
}
void Last(Tree *kk){
	if(kk!=NULL){
	Last(kk->left);
	Last(kk->right); 
	if(kk==root) printf("%d\n",kk->data);
	else printf("%d ",kk->data);
	}
	
}
int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
	root=NULL;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&pre[i]);
		}
		for(int i=0;i<n;i++)
		{
			scanf("%d",&mid[i]);
		}

	root=create(pre,mid,n); 
	Last(root);

	}
} 

https://www.cnblogs.com/weiyi-mgh/p/6616008.html
二叉树前序中序后序的推算

后续和中序 推前序
6
1 3 4 2 8 6
1 2 3 4 6 8

#include<bits/stdc++.h>
using namespace std;
int last[1010],mid[1010];
struct node{
	node* LL;
	node* RR;
	int data;
}*root;	
int n,k;
node * build(int*a,int *b,int nn){
	node *t;
	for(int i=0;i<nn;i++){
		if(a[nn-1]==b[i]){
	t=(node*)malloc(sizeof(node));
	t->data=a[nn-1];
	t->LL=build(a,b,i);
	t->RR=build(a+i,b+1+i,nn-i-1);	
	return t;
		}
	}
	return NULL; 
}
void pre( node *gg){
	if(gg!=NULL){
		++k;
		if(k!=n) {
			cout<<gg->data<<" ";
		}else {
			cout<<gg->data <<endl;	
		}
		pre(gg->LL);
		pre(gg->RR);
	} 
}
int main(){

	while(~scanf("%d",&n)){
		root=NULL;
		for(int i=0;i<n;i++){
			scanf("%d",&last[i]);
		}
		for(int i=0;i<n;i++){
			scanf("%d",&mid[i]);
		}
		root=build(last,mid,n);
		k=0;
		pre(root);
	}
}

6
1 3 4 2 8 6
1 2 3 4 6 8
ans=6 2 1 4 3 8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值