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
    }
  • 相关阅读:
    CSS BEM 命名规范简介
    React 端的编程范式
    在React应用程序中用RegEx测试密码强度
    React 中获取数据的 3 种方法及它们的优缺点
    vue props传值常见问题
    如何理解vue中的v-model?
    利用jQuery not()方法选取除某个元素外的所有元素
    初识Nest.js
    react-绑定this并传参的三种方式
    Angular怎么防御xss攻击?
  • 原文地址:https://www.cnblogs.com/MalcolmMeng/p/8442973.html
Copyright © 2011-2022 走看看