zoukankan      html  css  js  c++  java
  • PAT1119

    #include<cstdio>
    #include<vector>
    using namespace std;
    const int maxn=35;
    struct node{
        int k;
        struct node *lc;
        struct node *rc;
    };
    int n;
    int pre[maxn];
    int post[maxn];
    bool Unique=true;
    void create(node *&root,int preL,int preR,int postL,int postR)
    {
        if(preL>preR)return ;
        node *t=new node;
        int rootE=pre[preL];
        t->k=rootE;
        t->lc=t->rc=nullptr;
        root=t;
        if(preL==preR)return ;
        int nxtRoot=pre[preL+1];
        int i,numLeft=0;
        for(i=postL;i<=postR-1;i++){
            numLeft++;
            if(post[i]==nxtRoot){
                break;
            }
        }
        //if(i==postR-1)Unique=false;
        //当区间缩小到只有两个元素的时候判断唯一性
        //若出现例如34,43的情况就是不唯一
        if(pre[preL]==post[postR]
        &&pre[preR]==post[postL]){
            Unique=false;
            //此处可以控制产生树的形状
            create(root->rc,preL+1,preL+numLeft,postL,i);
            create(root->lc,preL+numLeft+1,preR,i+1,postR-1);
        }
        else {
            create(root->lc,preL+1,preL+numLeft,postL,i);
            create(root->rc,preL+numLeft+1,preR,i+1,postR-1);
        }
    }
    void PreOrder(node *root)
    {
        if(root==nullptr)return ;
        printf("%d",root->k);
        PreOrder(root->lc);
        PreOrder(root->rc);
    }
    void PostOrder(node *root)
    {
        if(root==nullptr)return ;
        PostOrder(root->lc);
        PostOrder(root->rc);
        printf("%d",root->k);
    }
    vector<int>v;
    void InOrder(node *root)
    {
        if(root==nullptr)return ;
        InOrder(root->lc);
        v.push_back(root->k);
        InOrder(root->rc);
    }
    //#define debug
    int main()
    {
    #ifdef debug
        freopen("in.txt","r",stdin);
    #endif
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&pre[i]);
        }
        for(int i=0;i<n;i++){
            scanf("%d",&post[i]);
        }
        node *root;
        create(root,0,n-1,0,n-1);
        InOrder(root);
        if(Unique)printf("Yes
    ");
        else printf("No
    ");
        for(int i=0;i<v.size();i++){
            printf("%d",v[i]);
            if(i!=v.size()-1)printf(" ");
            else printf("
    ");
        }
    #ifdef debug
        getchar(); 
    #endif
    }
  • 相关阅读:
    目标检测算法原理
    物体检测项目
    Bootstrap+Font Awesome图标不显示 或显示错误解决办法
    关于 微信发送被动回复音乐消息 用户接收不到的问题
    多线程操作SQLite注意事项
    SQLiteDatabase中query、insert、update、delete方法参数说明
    Android开发:使用Fragment改造TabActivity
    UDP广播与多播
    Android 布局文件 属性区别
    Android开发
  • 原文地址:https://www.cnblogs.com/MalcolmMeng/p/8442973.html
Copyright © 2011-2022 走看看