PAT Advanced1020 Tree Traversals(二叉树)

链接:PAT Advanced1020

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



题意:
给出某二叉树的后序遍历序列中序遍历序列,要求输出该二叉树的层序遍历序列


结论:中序遍历序列可以和先序序列后序序列层序序列中的任意一个来构成唯一的二叉树,而后三者两两搭配甚至三个一起上都无法构建唯一的二叉树。

ps.旨在再次熟悉一下二叉树的遍历根据中序序列×其他序列构建二叉树二叉树的两种实现


以下代码:


①二叉树的指针(动态)实现

#include<cstdio>
#include<queue>
using namespace std;
struct node
{
	int key;
	node* Left;
	node* Right;
};
int post[40], in[40],N;
node* creat(int postL, int postR, int inL, int inR)
{
	if (postL > postR)     //当postL>postR,表示该树为空,返回NULL
		return NULL;
	node* root = new node;
	root->key = post[postR];     //后序序列的最后一个为根
	int k;
	for (k = inL; k <= inR; k++)
	{
		if (in[k] == post[postR])    //在中序序列中找到根
			break;
	}
	int Ln = k - inL;        //算出左子树的结点个数
	root->Left = creat(postL, postL + Ln - 1, inL, k - 1);   //左子树递归
	root->Right = creat(postL + Ln, postR - 1, k + 1, inR);  //右子树递归
	return root;
}
void LayerOrder(node* root)   //层序遍历,即BFS
{
	queue<node*> q;    //注意,这里queue中存储的是结点地址。
	q.push(root);
	while (!q.empty())
	{
		node* t = q.front();
		q.pop();
		if (t!=root)
			printf(" ");
		printf("%d", t->key);
		if (t->Left != NULL)
			q.push(t->Left);
		if (t->Right != NULL)
			q.push(t->Right);
	}
}
int main()
{
	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]);
	LayerOrder(creat(0, N - 1, 0, N - 1));
	return 0;
}

②二叉树的数组(静态)实现

#include<cstdio>
#include<queue>
using namespace std;
struct node
{
	int key;
	int Left;
	int Right;
}Node[40];     //数组下标表示为结点地址
int post[40], in[40],N,index=0;
int creat(int postL, int postR, int inL, int inR)
{
	if (postL > postR)     //当postL>postR,该树为空,返回-1(表示空(NULL))
		return -1;
	int root = index++;    //取一个新下标(类比为地址)
	Node[root].key = post[postR];
	int k;
	for (k = inL; k <= inR; k++)
	{
		if (in[k] == post[postR]) 
			break;
	}
	int Ln = k - inL;
	Node[root].Left = creat(postL, postL + Ln - 1, inL, k - 1);
	Node[root].Right = creat(postL + Ln, postR - 1, k + 1, inR);
	return root;
}
void LayerOrder(int root)
{
	queue<int> q;   //这里queue中存储int类型,表示下标(类比为地址)
	q.push(root);
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		if (t!=root)
			printf(" ");
		printf("%d", Node[t].key);
		if (Node[t].Left != -1)    //相应的要 !=-1
			q.push(Node[t].Left);
		if (Node[t].Right != -1)
			q.push(Node[t].Right);
	}
}
int main()
{
	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]);
	LayerOrder(creat(0, N - 1, 0, N - 1));
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值