Tree Traversals(30行代码搞定!)

题目

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

答案

自己的原答案

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int n,post[10000],mid[10000];
vector<int> vec[35];

void bfs(int root,int left,int right)
{
	if(left>=right) return;
	int pos=-1,i;
	for(i=left;i<=right;i++)
	{
		if(mid[i]==post[root]) 
		{
			pos=i;
			break;
		}
	}
	int rlen=right-i;
	int llen=i-left;
	if(root-rlen-1==root-1)
		vec[root].push_back(root-rlen-1);
	else
	{
		if(llen>0)
			vec[root].push_back(root-rlen-1);
		if(rlen>0)
			vec[root].push_back(root-1);
	}
	
	bfs(root-rlen-1,right-rlen-llen,right-rlen-1);
	bfs(root-1,right-rlen+1,right);
}

void dfs(int root)
{
	queue<int> q;
	q.push(root);
	int last=root,flag=1;
	while(!q.empty())
	{
		int tmp=q.front();
		if(flag) flag=0;
		else cout<<" ";
		cout<<post[tmp];
		q.pop();
		for(int i=0;i<vec[tmp].size();i++)
		{
			q.push(vec[tmp][i]);
		}
		if(tmp==last) last=q.back();
	}
}

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	cin>>post[i];
	for(int i=1;i<=n;i++)
	cin>>mid[i];
	bfs(n,1,n);	
	dfs(n);
} 

30行代码的答案

#include<iostream>
#include<map>
using namespace std;
int n,post[35],mid[35],flag=1;
map<int,int> level;
void pre_order(int mid_start,int mid_end,int post_end,int index)
{
	if(mid_start>mid_end) return;
	int i;
	for(i=mid_start;i<=mid_end;i++)
	if(post[post_end]==mid[i]) break;
	level[index]=mid[i];
	pre_order(mid_start,i-1,post_end-1-(mid_end-i),index*2+1);
	pre_order(i+1,mid_end,post_end-1,index*2+2);
}
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	cin>>post[i];
	for(int i=0;i<n;i++)
	cin>>mid[i];
	pre_order(0,n-1,n-1,0);
	for(map<int,int>::iterator it=level.begin();it!=level.end();it++)
	{
		if(flag) flag=0;
		else cout<<" ";
		cout<<it->second;
	}
}

总结

第二个答案我参考了这篇文章——PAT 1020 Tree Traversals (25分)思路分析 + 满分代码

参考文章的思路相当于在按照前序排序的同时,加入一个index变量记录顺序(下标),并将index和它对应的值存入创建好的map变量中, 最后因为map默认是按照键值(index)从小到大排列,所以只需遍历map变量即可

和这个思路相比,我原来的思路确实太麻烦了,所以我果断向优秀的代码学习!

希望这篇文章能对正在观看的你有所帮助!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值