1020 Tree Traversals (25 分)-PAT甲级

该博客介绍了如何通过后序和中序遍历来构造二叉树,并实现层次遍历。首先,递归地从后序和中序遍历得到前序遍历,然后修改该方法以获取层次遍历。此外,还提供了AC代码实现这一过程。更新后的解决方案包括直接根据后序和中序遍历构造二叉树,然后进行层序遍历。
摘要由CSDN通过智能技术生成

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

 思路:1.本人开始写这道题的时候没有任何思路,本题思路参考柳婼大神,以下仅对其思路进行描述。

        2.采用递归实现从后序和中序得到层次遍历,是在递归实现从后序和中序得到前序遍历改进得来的。

        3.递归实现从后序和中序得到前序遍历:根在后序遍历中最后输出,在前序遍历中最先输出,因此找到后序遍历中的根并将其最先输出即可。在每一次递归过程中仅仅是因为数据规模的不同(树和子树的关系),执行方法是一样的。具体方法为每次递归找出后序中的根(位置记为root)在中序遍历中对应的下标i,下一次先对左子树进行递归,根节点在后序遍历中的下标为root-(end-i+1),即当前根节点-(右子树的个数+1),左子树遍历完后对右子树进行遍历,根节点在后序遍历中的下标为root-1,,无论是左子树递归还是右子树递归,begin都是左(右)子树的起点,end都是左(右)子树的终点。

4.递归实现从后序和中序得到层次遍历:定义一个层次遍历位置的下标index,用map<int,int> level实现index对应index下标位置的值(map可以实现按照index大小排序)index位置的左子树位置为index*2+1,右子树位置为index*2+2,将前序遍历代码修改,即可得到层次遍历代码。

后序和中序得到前序

#include <iostream>
#include <vector>
using namespace std;
vector <int> post,in;
void pre(int root,int start,int end)
{
	if(start>end)
		return;	
	cout<<post[root]<<" ";
	int i=start;
	while(i<end&&post[root]!=in[i])
		i++;
	pre(root-(end-i+1),start,i-1);
	pre(root-1,i+1,end);
} 
int main()
{
	int n,temp,index=0;
	cin>>n;
	for(int i=0;i<n*2;i++)
	{
		cin>>temp;
		if(i<n)
			post.push_back(temp);
		else
			in.push_back(temp);
	}
	pre(n-1,0,n-1);
	return 0;
}

AC代码(后序和中序得到层次)

#include <iostream>
#include <map>
#include <vector>
using namespace std;
vector <int> post,in;
map<int,int> level;
void pre(int root,int start,int end,int index)
{
	if(start>end)
		return;	
	int i=start;
	level[index]=post[root];
	while(i<end&&post[root]!=in[i])
		i++;
	pre(root-(end-i+1),start,i-1,index*2+1);
	pre(root-1,i+1,end,index*2+2);
} 
int main()
{
	int n,temp,index=0;
	cin>>n;
	for(int i=0;i<n*2;i++)
	{
		cin>>temp;
		if(i<n)
			post.push_back(temp);
		else
			in.push_back(temp);
	}
	pre(n-1,0,n-1,0);
	map<int,int>::iterator it;
	for(it=level.begin();it!=level.end();it++)
	{
        if(it!=level.begin())
			cout<<" ";
		cout<<it->second;
	} 
	return 0;
}

2022年3月24日更新

本题还可以根据后序和中序遍历构造唯一一颗二叉树,对构造的二叉树进行层序遍历即可。

#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std; 
struct tree {
	int data;
	tree* lchild;
	tree* rchild;
};
vector<int> post, in, ans; 
map<int, int> index;
void createTree(int data, tree* &root) {
	if (root == NULL) {
		root = new tree;
		root->data = data;
		root->lchild = NULL;
		root->rchild = NULL;
		return;
	}
	if (index[root->data] > index[data]) createTree(data, root->lchild);
	if(index[root->data] <= index[data]) createTree(data,root->rchild);
}
void levelorder(tree* root) {
	queue<tree*> q;
	q.push(root);
	while (!q.empty()) {
		ans.push_back((q.front())->data);
		if ((q.front())->lchild != NULL) q.push((q.front())->lchild);
		if ((q.front())->rchild != NULL) q.push((q.front())->rchild);
		q.pop();
	}
}
int main() {
	int N;
	cin >> N;
	post.resize(N);
	in.resize(N);
	for (int i = 0; i < N; i++) cin >> post[i];
	for(int i = 0; i < N; i++) {
		cin >> in[i];
		index[in[i]] = i;
	}
	tree* root = NULL;
	for (int i = N - 1; i >= 0; i--) createTree(post[i], root);
	levelorder(root);
	for (int i = 0; i < ans.size(); i++) {
		if (i != 0) cout << " ";
		cout << ans[i]; 
	} 
	return 0;
}



 更多PAT甲级题目:请访问PAT甲级刷题之路--题目索引+知识点分析(正在进行),感谢支持!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值