【PAT甲级】1020 Tree Traversals

10 篇文章 0 订阅
3 篇文章 0 订阅

题目描述:

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

题目翻译:

 给你一颗二叉树的后序和中序遍历,求该二叉树的层次遍历


 首先复习一下

一:中序+先序遍历求后序遍历

由于先序遍历是根左右,所以我们从先序遍历得到的序列的头部来获取根节点

然后把根节点记录下来,并且在中序序列中找到根节点位置,把原序列分割成左右子树

由于要求后序遍历,我们要先遍历完左右子树 再输出根节点

void Post(int start, int end, int root)
{
	if (start > end)
	{
		return;
	}

	int i = start;

	while (i < end && in[i] != pre[root])
	{
		i++;
	}

	Post(start, i - 1, root + 1);//先遍历左子树
	Post(i + 1, end, root + 1 + i - start);//再遍历右子树
	cout << pre[root] << " ";//最后输出根节点
}

 代码这里注意的是

函数参数的root是根节点在先序序列的位置,即在pre的位置

而start和end都是在中序序列的位置,子树的范围


二:中序+后序遍历求先序

和求后序原理差不多,稍微改动即可

这里要注意的是后序遍历顺序是左右根,所以根节点在序列的尾部,我们从序列尾部往前读

另外,先序输出顺序是根左右,所以改动一下输出顺序即可

void Pre(int start, int end, int root)
{
	if (start > end)
	{
		return;
	}

	int i = start;

	while (i < end && in[i] != post[root])
	{
		i++;
	}

	cout << in[i] << " ";//先输出根节点
	Pre(start, i - 1, root - 1 - end + i);//在遍历左子树
	Pre(i + 1, end, root - 1);//最后遍历右子树
}

三:后序+中序遍历求层次遍历(题目所求)

对于层次遍历,我们可以给二叉树中每个节点标号,即赋予其下标。

从根节点i开始,其左右子树分别为2*i+1和2*i+2(从1开始)(从0开始是2*i和2*i+1)

然后用map把节点存储,由于map自动排序,可以方便输出

void level(int start, int end, int root, int index)
{
	if (start > end)
	{
		return;
	}

	int i = start;

	while (i < end && in[i] != post[root])//找到root在in的位置
	{
		i++;
	}

	m[index] = post[root];

	level(start, i - 1, root - 1 - end + i, 2 * index + 1);
	level(i + 1, end, root - 1, 2 * index + 2);
}

参数多一个index来记录下标


 ac代码如下:
 

#include <bits/stdc++.h>
using namespace std;

map<int, int> m;

vector<int>post, in, pre;

void level(int start, int end, int root, int index)
{
	if (start > end)
	{
		return;
	}

	int i = start;

	while (i < end && in[i] != post[root])//找到root在in的位置
	{
		i++;
	}

	m[index] = post[root];

	level(start, i - 1, root - 1 - end + i, 2 * index + 1);
	level(i + 1, end, root - 1, 2 * index + 2);
}

int main()
{
	int sum;
	cin >> sum;

	post.resize(sum);
	in.resize(sum);

	for (int i = 0; i < sum; i++)
	{
		cin >> post[i];
	}

	for (int i = 0; i < sum; i++)
	{
		cin >> in[i];
	}

	level(0, sum - 1, sum - 1, 0);

	auto it = m.begin();

	cout << it->second;

	while (++it != m.end())
	{
		cout<<" "<<it->second;
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值