编程小白刷PAT(A1020 Tree Traversals)

已知后序和中序,求层序序列。

刚开始想模仿先序和中序重建二叉树的代码,使用string存储先序和中序序列,以便使用string的find函数和substr函数。

错误代码:只通过第1、4测试点。

#include<cstdio>
#include<string>
#include<vector>
#include<queue>
using namespace std;
struct TreeNode{
    char data;
    TreeNode * leftchild;
    TreeNode * rightchild;
};
TreeNode * Build(string In,string Post){
    if(In.size()==0 || Post.size()==0){
        return NULL;
    }
    else{
        char cdata=Post[Post.size()-1];
        int pos=In.find(cdata);
        TreeNode * NewNode=new TreeNode;
        NewNode->data=cdata;
        NewNode->leftchild=Build(In.substr(0,pos),Post.substr(0,pos));
        NewNode->rightchild=Build(In.substr(pos+1),Post.substr(pos,Post.size()-1-pos));
        return NewNode;
    }
}
void CxOrder(TreeNode * root){
    queue<TreeNode *> myqueue;
    vector<char> vec;
    if(root!=NULL){
        myqueue.push(root);
        while(!myqueue.empty()){
            TreeNode * nNode=myqueue.front();
            myqueue.pop();
            vec.push_back(nNode->data);
            //printf("%c ",nNode->data);
            if(nNode->leftchild!=NULL){
            myqueue.push(nNode->leftchild);
            }
            if(nNode->rightchild!=NULL){
            myqueue.push(nNode->rightchild);
            }
        }
        for(int i=0;i<vec.size();i++){
            if(i!=0){
                printf(" ");
            }
            printf("%c",vec[i]);
        }
    }
}
int main(){
    int N;
    char c;
    scanf("%d",&N);
    getchar();
    string In;
    string Post;
    for(int i=0;i<N;i++){
        scanf("%c",&c);
        getchar();
        Post.push_back(c);
    }
    for(int i=0;i<N;i++){
        scanf("%c",&c);
        getchar();
        In.push_back(c);
    }
    TreeNode * root=Build(In,Post);
    CxOrder(root);
    getchar();
    getchar();
    return 0;
}

二叉树结点存储的数据类型改为int,并且在全局使用vector向量存储中序和后序序列。

注意:

        NewNode->data=cdata;
        int length=Inright-pos;
        NewNode->leftchild=Build(Inleft,pos-1,Postleft,Postright-length-1);
        NewNode->rightchild=Build(pos+1,Inright,Postright-length,Postright-1);

正确代码:

#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
vector<int> In;
vector<int> Post;
struct TreeNode{
    int data;
    TreeNode * leftchild;
    TreeNode * rightchild;
};
TreeNode * Build(int Inleft,int Inright,int Postleft,int Postright){
    int pos;
    if(Inleft>Inright || Postleft>Postright){
        return NULL;
    }
    else{
        int cdata=Post[Postright];

        for(int i=Inleft;i<=Inright;i++){
            if(cdata==In[i]){
                pos=i;
                break;
            }
        }
        TreeNode * NewNode=new TreeNode;
        NewNode->data=cdata;
        int length=Inright-pos;
        NewNode->leftchild=Build(Inleft,pos-1,Postleft,Postright-length-1);
        NewNode->rightchild=Build(pos+1,Inright,Postright-length,Postright-1);
        return NewNode;
    }
}
void CxOrder(TreeNode * root){
    queue<TreeNode *> myqueue;
    vector<int> vec;
    if(root!=NULL){
        myqueue.push(root);
        while(!myqueue.empty()){
            TreeNode * nNode=myqueue.front();
            myqueue.pop();
            vec.push_back(nNode->data);
            if(nNode->leftchild!=NULL){
                myqueue.push(nNode->leftchild);
            }
            if(nNode->rightchild!=NULL){
                myqueue.push(nNode->rightchild);
            }
        }
        for(int i=0;i<vec.size();i++){
            if(i!=0){
                printf(" ");
            }
            printf("%d",vec[i]);
        }
        printf("\n");
    }
}
int main(){
    int N;
    int c;
    scanf("%d",&N);
    getchar();
    for(int i=0;i<N;i++){
        scanf("%d",&c);
        getchar();
        Post.push_back(c);
    }
    for(int i=0;i<N;i++){
        scanf("%d",&c);
        getchar();
        In.push_back(c);
    }
    TreeNode * root=Build(0,In.size()-1,0,Post.size()-1);
    CxOrder(root);
    getchar();
    getchar();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值