PAT备考日记-树篇(1)-PAT甲级1086

几乎躺尸了整个寒假,幸运的是PAT往后推迟了,不幸的是PAT推迟的时间太多了,推到了夏令营集中的时间,不知道自己还有没有机会参加,但为了机试什么的,代码一定要操练起来。

根据大佬的统计,PAT中有关树的考察是非常多的,包括树的遍历、树的性质、前中后层序遍历、BST、AVL等等。由于自己代码能力非常弱,先从最简单的二叉树做起。

事先需要了解的:

了解递归、队、书的存储方式、树的遍历方式等基本知识。对应《算法笔记》的第八章、第九章。

PAT甲级1086

题的大意是 由push xx;pop;等语句所构造的一棵二叉树,你要输出该二叉树的后序遍历序列

解1:
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<string>
#include<string.h>
#include<iostream>
using namespace std; 

struct node{
	int data;
	node* lchild;
	node* rchild;
};

int n,postf=0,pref=0,k=0;
int postres[65],pre[65];
string str;
string pop="Pop";

node* newnode(int x){
	node* temp=new node;
	temp->data=x;
	temp->lchild=NULL;
	temp->rchild=NULL;
	return temp;
}

void initpre(int n){//存储先序遍历的结果
	for(int i=0;i<2*n;i++){
		cin>>str;
		if(str!=pop){
			cin>>str;
			int str_int=stoi(str);
			pre[pref++]=str_int;
		}
		else{
			pre[pref++]=-1;
		}
	}
}

void postorder(node* root){//后序遍历
	if(root==NULL) return ;
	postorder(root->lchild);
	postorder(root->rchild);
	postres[postf++]=root->data;
}

void inittree(node* &root){//通过先序序列生成树
	if(pre[k]==-1) {
		k++;
	    root = NULL;
	}
	else{
		root=newnode(pre[k++]);
        inittree(root->lchild);
        inittree(root->rchild);
	}
}

int main(){
	scanf("%d",&n);
	initpre(n);
	pre[pref++]=-1;//添加最后一个空结点
	node* root=NULL;
	inittree(root);
	postorder(root);
	for(int i=0;i<postf;i++){
		printf("%d",postres[i]);
		if(i!=postf-1) printf(" ");
	}
	return 0;
}

主要思路: 观察题干,不难看出如果将pop看作该点为空,那么push pop将会变成,先序遍历的序列,所以我们可以直接通过这个先序序列构造树,然后输出其后序遍历。

需要注意的是:因为有n个点,树中应有n+1个空结点,但题干中只有n个空结点,忽略了最后一个,最后整理先序遍历序列时要加上最后的空结点

解2:
#include<stdio.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<stdlib.h>
using namespace std;

int n,postf=0;
int pre[65],in[65],post[65],pref=0,inf=0,postr=0;
string str;
int strint;
stack<int> s;

struct node{
	int data;
	node* lchild;
	node* rchild;
};

node* create(int prel,int prer,int inl,int inr){
	if(prel>prer) return NULL;
	node* node1=new node;
	node1->data=pre[prel];
	int k;
	for(k=inl;k<=inr;k++){
		if(in[k]==pre[prel]) break;
	}
	int numleft=k-inl;
	node1->lchild=create(prel+1,prel+numleft,inl,k-1);
	node1->rchild=create(prel+numleft+1,prer,k+1,inr);
	return node1;
} 
void postorder(node* root){
	if(root==NULL) return;
	postorder(root->lchild);
	postorder(root->rchild);
	post[postr++]=root->data;
}

int main(){
	scanf("%d",&n);
	for(int i=0;i<2*n;i++){
		cin>>str;
		if(str.size()!=3){
			cin>>strint;
			pre[pref++]=strint;
			s.push(strint);
		}
		else{
			int now=s.top();
			s.pop();
			in[inf++]=now;
		}
	}
    node* root=create(0,n-1,0,n-1);
	postorder(root);
	for(int i=0;i<postr;i++){
		printf("%d",post[i]);
		if(i!=postr-1) printf(" ");
	}
	return 0;
}

 

主要思路: 其中push的顺序是先序,pop出的顺序是中序,所以该题变成了通过先序,中序得到后序的模板题,《算法笔记》中有详解。

后记:

在该模板题中,熟悉了二叉树的基本玩法,包括二叉树的不同存储形式、如何通过完整的先序序列生成一棵二叉树、如何已知先序中序推出后序、基本的遍历…

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值