树的遍历

7-11 玩转二叉树 (25分)
给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数。

输入格式:
输入第一行给出一个正整数N(≤30),是二叉树中结点的个数。第二行给出其中序遍历序列。第三行给出其前序遍历序列。数字间以空格分隔。

输出格式:
在一行中输出该树反转后的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:
7
1 2 3 4 5 6 7
4 1 3 2 6 5 7
输出样例:
4 6 1 7 5 3 2

#include<iostream>
#include<cmath>
#include<string>
#include<queue>
#include<cstring>
#include<list>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<algorithm>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#define fcio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define lowbit(x) (x&-x)
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
int pre[maxn];
int in[maxn];
int pos;
struct node{
	int l,r,w;
}tree[maxn];
void build(int l,int r,int n){
	if(l==r){
		tree[n].w=-1;
		return;
	}
	int root=pre[pos++];
	tree[n].w=root;
	tree[n].l=2*n;
	tree[n].r=2*n+1;
	int mid=find(in+1,in+r,root)-in;
	build(l,mid,2*n);
	build(mid+1,r,2*n+1);
}
void bfs(){
	queue<int>q;
	q.push(1);
	int s;
	while(!q.empty()){
		s=q.front();
		q.pop();
		if(tree[s].w!=-1){
			if(s!=1)
				cout<<" ";
			cout<<tree[s].w;
			q.push(tree[s].r);
			q.push(tree[s].l);
		}
	}
	cout<<endl;
	return ;
}
int main(){
	fcio;
	int n;
	while(cin>>n){
		pos=1;
		for(int i=1;i<=n;++i)
			cin>>in[i];
		for(int i=1;i<=n;++i)
			cin>>pre[i];
		build(1,n+1,1);
		bfs();
	}
	return 0;
}

7-10 树的遍历 (25分)
给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。

输入格式:
输入第一行给出一个正整数N(≤30),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。

输出格式:
在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
输出样例:
4 1 6 3 5 7 2

#include<iostream>
#include<cmath>
#include<string>
#include<queue>
#include<cstring>
#include<list>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<algorithm>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#define fcio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define lowbit(x) (x&-x)
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
int post[maxn],in[maxn];
int rch[maxn],lch[maxn];
int build(int l1,int r1,int l2,int r2){
	if(l1>r1||l2>r2)
		return -1;
	int root=post[r1];
	int p=0;
	while(in[l2+p]!=root)
		p++;
	lch[root]=build(l1,l1+p-1,l2,l2+p-1);
	rch[root]=build(l1+p,r1-1,l2+p+1,r2);
	return root;
}
void bfs(int root){
	queue<int>q;
	q.push(root);
	while(!q.empty()){
		//cout<<"***********"<<endl;
		int tn=q.front();
		cout<<tn;
		q.pop();
		if(lch[tn]>0)
			q.push(lch[tn]);
		if(rch[tn]>0)
			q.push(rch[tn]);
		if(q.size()!=0)
			cout<<" ";
	}
	return ;
}
int main(){
	int n;
	while(cin>>n){
		memset(rch,-1,sizeof rch);
		memset(lch,-1,sizeof lch);
		for(int i=0;i<n;++i)
			cin>>post[i];
		for(int i=0;i<n;++i)
			cin>>in[i];
		//cout<<"***********"<<endl;
		int root=build(0,n-1,0,n-1);
		//cout<<"***********"<<endl;
		bfs(root);
	}
	return 0;
}
//输入样例:
//7
//2 3 1 5 7 6 4
//1 2 3 4 5 6 7
//输出样例:
//4 1 6 3 5 7 2

P1827 [USACO3.4]美国血统 American Heritage

题目描述
农夫约翰非常认真地对待他的奶牛们的血统。然而他不是一个真正优秀的记帐员。他把他的奶牛 们的家谱作成二叉树,并且把二叉树以更线性的“树的中序遍历”和“树的前序遍历”的符号加以记录而 不是用图形的方法。

你的任务是在被给予奶牛家谱的“树中序遍历”和“树前序遍历”的符号后,创建奶牛家谱的“树的 后序遍历”的符号。每一头奶牛的姓名被译为一个唯一的字母。(你可能已经知道你可以在知道树的两 种遍历以后可以经常地重建这棵树。)显然,这里的树不会有多于 26 个的顶点。 这是在样例输入和 样例输出中的树的图形表达方式:

在这里插入图片描述

树的中序遍历是按照左子树,根,右子树的顺序访问节点。

树的前序遍历是按照根,左子树,右子树的顺序访问节点。

树的后序遍历是按照左子树,右子树,根的顺序访问节点。

输入格式
第一行: 树的中序遍历

第二行: 同样的树的前序遍历

输出格式
单独的一行表示该树的后序遍历。

输入输出样例
输入
ABEDFCHG
CBADEFGH
输出
AEFDBHGC

#include<iostream>
#include<cmath>
#include<string>
#include<queue>
#include<cstring>
#include<list>
#include<map>
#include<set>
#include<cstdio>
#include<vector>
#include<stack>
#include<algorithm>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#include<sstream>
#define fcio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define lowbit(x) (x&-x)
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
const int INF=0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
string in,pre,past;
void dfs(int l1,int r1,int l2,int r2){
	if(l1>r1||l2>r2)
		return;
	for(int i=l1;i<=r1;++i){
		if(in[i]==pre[l2]){
			dfs(l1,i-1,l2+1,l2+i-l1);
			dfs(i+1,r1,l2+i-l1+1,r2);
			cout<<in[i];
		}
	}
}
int main(){
//	fcio;
	while(cin>>in){
		cin>>pre;
		int len=in.length();
		dfs(0,len-1,0,len-1);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值