【2019暑期】【PAT甲级】树专练总结

48 篇文章 0 订阅

树默认0开存

写前中后序

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;

int tree[510],n;
void preorder(int root){
	if(tree[root] == -1){
		return ;
	}
	if(root==0) cout << tree[root];
	else cout << " " << tree[root];
	preorder(root*2+1);
	preorder(root*2+2);
}

void inorder(int root){
	if(tree[root]==-1) return ;
	inorder(root*2+1);
	if(root==0) cout << tree[root];
	else cout << " " << tree[root];
	inorder(root*2+2);
}

void postorder(int root){
	if(tree[root]==-1) return ;
	postorder(root*2+1);
	postorder(root*2+2);
	cout << tree[root] << " ";
}

int main()
{
	cin >> n;
	fill(tree,tree+510,-1);
	for(int i=0; i<n; i++){
		int a;
		cin >> a;
		tree[i] = a;
	}
	
	//输出前序 中左右 
	preorder(0); 
	//输出中序 左中右
	inorder(0);
	//输出后序 左右中
	postorder(0); 
	
    return 0;
}

已知前中序写后序

用下标,注意返回值

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;

int n;
int in[51000],pre[51000];
struct tr{
	int lc,rc,id;
};
vector<tr> tree(51000);
vector<int> post;

int build(int prel, int inl, int inr){
	if(inl > inr) return -1;
	int i = inl;
	int root = prel;
	while(i<inr&&in[i]!=pre[prel]) i++;
	tree[root].lc = build(prel+1,inl,i-1);
	tree[root].rc = build(prel+i-inl+1,i+1,inr);
	tree[root].id = pre[prel];
	return root;
}

void postorder(int root){
	if(tree[root].lc!=-1)
	postorder(tree[root].lc);
	if(tree[root].rc!=-1)
	postorder(tree[root].rc);
	post.push_back(tree[root].id);
	
}

int main()
{
	cin >> n;
	for(int i=0; i<n; i++){
		cin >> pre[i];
	}
	for(int i=0; i<n; i++){
		cin >> in[i];
	}
	build(0,0,n-1);
	postorder(0);
	for(int i=0; i<n; i++){
		cout << post[0] << endl;
	}
	
    return 0;
}

已知后序中序写前序

注意是从n-1进行递归

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;

int n;
int in[51000],post[51000];
struct tr{
	int lc,rc,id;
};
vector<tr> tree(51000);
vector<int> pre;

int build(int postl, int inl, int inr){
	if(inl>inr) return -1;
	int root = postl;
	int i = inl;
	while(i<inr&&in[i]!=post[postl]) i++;
	tree[root].lc = build(postl-1-inr+i,inl,i-1);
	tree[root].rc = build(postl-1,i+1,inr);
	tree[root].id = post[root];
	return root;
}
void preorder(int root){
	pre.push_back(tree[root].id);
	if(tree[root].lc!=-1) preorder(tree[root].lc);
	if(tree[root].rc!=-1) preorder(tree[root].rc);
	
}
int main()
{
	cin >> n;
	for(int i=0; i<n; i++){
		cin >> post[i];
	}
	for(int i=0; i<n; i++){
		cin >> in[i];
	}
	build(n-1,0,n-1);
	preorder(n-1);
	cout << endl;
	for(int i=0; i<n; i++){
		cout << pre[i] << endl;
	}
    return 0;
}

前序后序写中序

void build(int prel, int prer, int postl, int postr) {
	
	if (prel == prer) {
		in.push_back(pre[prel]);
		return;
	}
	
	if (pre[prel] == post[postr]) {
		int i = prel + 1;
		while (i <= prer && pre[i] != post[postr-1]) i++;
		if (i-prel > 1){
			build(prel+1, i-1, postl, postl+(i-prel-1)-1);
					
		} else {
			key = false;
		}
		in.push_back(pre[prel]);
		build(i, prer, postl+(i-prel-1), postr-1);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值