PAT A1020

发布时间:2019-06-14 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PAT A1020脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

clipboard.png
之前总结过相应的东西。。。但是又给忘了,果然还是需要多次重复;

本题主要考虑两个点:四种遍历和遍历序列生成树;
后序序列+中序序列可以确定出唯一一颗子树,并且通过观察我们也可以发现两序列的判别点:
1.后续序列的其中一颗子树序列的最后一个节点必定是该子树的根节点;
2.通过该根节点可以将中序序列分为左右两个子树,通过子树序列再去第一步进行子树序列的判断;

之前学习的构建方法都是采用递归对数组索引进行操作;
如下代码所示:

node* create(int postl,int postr,int inl,int inr){
    if(postl>postr){
        return NULL;
    }
    node* root=new node;
    root->data=post[postr];
    int k;
    for(k=inl;k<inr;k++){
        //寻找根节点
        if(in[k]==post[postr]){
            break;
        }
    }
    int numleft=k-inl;
    root->lchild=create(postl,postl+numleft-1,inl,k-1);
    root->rchild=create(postl+numleft,postr-1,k+1,inr);
    return root;
}

其中需要注意的是递归边界,能够到达递归边界的是子树序列为1的情况,此时,如果对该节点进行构造,左右子树进入递归函数时,必定有postl>postr的情况出现;
并且注意左右子树递归的索引计算,后序序列左子树右子树序列的个数是根据中序序列中的左右子树个数来进行界定的;

层序也很简单,说到底就是BFS遍历;
总体代码如下所示:

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<queue>
using namespace std;
const int maxn=50;
struct node{
    int data;
    node* lchild;
    node* rchild;
};
int pre[maxn];//先序
int in[maxn];//中序
int post[maxn];//后续
int n;

node* create(int postl,int postr,int inl,int inr){
    if(postl>postr){
        return NULL;
    }
    node* root=new node;
    root->data=post[postr];
    int k;
    for(k=inl;k<inr;k++){
        //寻找根节点
        if(in[k]==post[postr]){
            break;
        }
    }
    int numleft=k-inl;
    root->lchild=create(postl,postl+numleft-1,inl,k-1);
    root->rchild=create(postl+numleft,postr-1,k+1,inr);
    return root;
}

int num=0;
void BFS(node* root){
    queue<node*>q;
    q.push(root);
    while(!q.empty()){
        node* now=q.front();
        q.pop();
        printf("%d",now->data);
        num++;
        if(num<n)
            printf(" ");
        if(now->lchild!=NULL)
            q.push(now->lchild);
        if(now->rchild!=NULL)
            q.push(now->rchild);
    }
}

int main(){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&post[i]);
    }
    for(int i=0;i<n;i++){
        scanf("%d",&in[i]);
    }
    node* root=create(0,n-1,0,n-1);
    BFS(root);
    system("pause");
    return 0;
}

脚本宝典总结

以上是脚本宝典为你收集整理的PAT A1020全部内容,希望文章能够帮你解决PAT A1020所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: