zoukankan      html  css  js  c++  java
  • PAT L3-010【完全二叉树】

    静态建树判一下1-n是不是为空就好了,如果有空的  就说明不是complete binary tree
    (和线段树建树差不多啊)Left=2*root;Right=2*root+1

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    
    struct BT{
      int Left;
      int Right;
      int w;
    }q[2000100];
    bool vis[2000100];
    
    void Build(int root,int x)
    {
      if(!vis[root])
      {
        vis[root]=true;
        q[root].w=x;
        q[root].Left=2*root;
        q[root].Right=2*root+1;
        return;
      }
      if(q[root].w>x)
        Build(2*root+1,x);
      else
        Build(2*root,x);
    }
    
    int main()
    {
      int n,x;
      memset(vis,false,sizeof(vis));
      scanf("%d",&n);
      for(int i=0;i<n;i++)
      {
        scanf("%d",&x);
        Build(1,x);
      }
      bool flag=false;
      queue<int>que;
      que.push(1);
      while(!que.empty())
      {
        int x=que.front();que.pop();
        if(flag) printf(" ");
        printf("%d",q[x].w);
        flag=true;
        int Left=2*x;
        if(vis[Left]) que.push(Left);    
        int Right=2*x+1;
        if(vis[Right]) que.push(Right);
    
      }
      
      puts("");
      for(int i=1;i<=n;i++)
        if(!vis[i]){
          puts("NO");
          return 0;
        }
      puts("YES");
      return 0;
    }


  • 相关阅读:
    杭电2042
    杭电2041
    杭电2040
    杭电2046
    SPOJ
    SPOJ
    SPOJ
    HDU
    HDU
    HDU
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/6777347.html
Copyright © 2011-2022 走看看