pta-1020 Tree Traversals(25 分)(已知后序中序求层序二叉树遍历与重建)

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

首先 ,让我们学一下英语....难过脸<<

NLR:前序遍历(Preorder Traversal 亦称(先序遍历))

LNR:中序遍历(Inorder Traversal)

LRN:后序遍历(Postorder Traversal)

题意:给出后序和中序遍历结果,输出先序遍历结果。

注意这题是算层序!!!!!!!!!!!!!不是先序(如果是先序,看注释掉的部分)

分析:后序序列的最后一个结点为整棵树的根节点,而对应的中序序列的根节点在中序序列的中间,此例为4,所以就将树分成左子树(1 2 3)和右子树(5 6 7)。

设中序序列为in,后序序列为post,设置一个游标变量cur,左右范围变量left、right,cur作为一个全局变量,每次在post中取出一个根,就让cur自减1,首先把拿到的根定位在inOrder中,设根所在的索引为Index,首先建立当前根节点T,然后生成左子树范围left到Index-1和右子树范围Index+1到right,注意!!由于后序序列倒着走线碰到右子树,因此应该先递归T->right,再递归T->left,当发现left比right大时,说明没有子树,直接返回NULL,当发现left=right时,说明这是一个叶子结点,将结点的left和right置为NULL然后返回,最后一次递归返回时返回的是第一次创建的根结点,也就是整棵树的根,这时便得到了完整的二叉树。

#include<bits/stdc++.h>
using namespace std;
const int maxn=35;
int post[maxn],in[maxn],cur,n,flag=1;
typedef struct TreeNode{
	struct TreeNode* left;
	struct TreeNode* right;
	int val;
}*tree,node;/////////////////
int findIndex(int x)
{
	for(int i=0;i<n;i++)
		if(in[i]==x)return i;
	return -1;
}
void build(tree &T,int left,int right)
{
	if(right<left)return ;
	int root=post[cur];
	cur--;
	int Index=findIndex(root);
	T=new node();
	T->val=root;
	if(right==left)//叶子结点 
	{
		T->left=NULL;
		T->right=NULL;
	}
	else{
		build(T->right,Index+1,right);
		build(T->left,left,Index-1);////-1
	}
}
void level(tree &T)
{
	if(T==NULL)return;
	queue<tree>q;
	q.push(T);
	while(!q.empty())
	{
		tree c=q.front();
		q.pop() ;
		if(flag)
		{
			printf("%d",c->val);
			flag=0;
		}
		else{
			printf(" %d",c->val);
		}
		if(c->left!=NULL)q.push(c->left);//////////////
		if(c->right!=NULL)q.push(c->right);////////////
	}
/*	if(T)
	{
		if(flag)
		{
			printf("%d",T->val);
			flag=0;
		}
		else{
			printf(" %d",T->val);
		}
       	level(T->left);
    	level(T->right);
	}
*/
}
int main()
{
	tree T;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		scanf("%d",&post[i]);
	for(int i=0;i<n;i++)
		scanf("%d",&in[i]);
	cur=n-1;
	build(T,0,n-1);
	level(T);
	cout<<endl;
	return 0;
}

已知先序中序求后序看这里~https://blog.csdn.net/qq_38735931/article/details/82113331

二叉树遍历模板看这里~https://blog.csdn.net/qq_38735931/article/details/82110307

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/82081966