静态建树判一下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;
}

PAT L3-010【完全二叉树】的更多相关文章

  1. PAT天梯赛练习题 L3-010. 是否完全二叉搜索树(完全二叉树的判断)

    L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...

  2. PAT A1110 Complete Binary Tree (25 分)——完全二叉树,字符串转数字

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  3. PAT A1123 Is It a Complete AVL Tree (30 分)——AVL平衡二叉树,完全二叉树

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  4. PAT A1155 Heap Paths (30 分)——完全二叉树,层序遍历,特定dfs遍历

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  5. PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)

    题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...

  6. PAT 1110 Complete Binary Tree[判断完全二叉树]

    1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...

  7. PAT甲级——1110 Complete Binary Tree (完全二叉树)

    此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830   1110 Complete Binary ...

  8. PAT A1147 Heaps (30 分)——完全二叉树,层序遍历,后序遍历

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  9. pat甲级题解(更新到1013)

    1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...

随机推荐

  1. php发邮件:swiftmailer, php邮件库——swiftmailer

    php发邮件:swiftmailer, php邮件库——swiftmailer 最近看到一个好的php邮件库,与phpmailer作用一样,但性能比phpmailer好,尤其是在处理附件的能力上,发送 ...

  2. Python习题-一个函数实现读写功能

    def new_op_file(filename,content=None): f = open(filename,'a+') f.seek(0) if content: #非空即真,如果有内容就往下 ...

  3. PHP 常量、PHP 变量全解析(超全局变量、变量的8种数据类型等)

    常量特点 常量一旦被定义就无法更改或撤销定义. 常量名不需要开头的$ 与变量不同,常量贯穿整个脚本是自动全局的. 作用域不影响对常量的访问 常量值只能是字符串或数字 设置 PHP 常量 如需设置常量, ...

  4. 【二叉树的递归】02二叉树的最大深度【Maximum Depth of Binary Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...

  5. 【遍历二叉树】03二叉树的后序遍历【Binary Tree Postorder Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的后序遍历的 ...

  6. luogu1833 樱花

    背包问题小合集 01背包 完全背包 多重背包混着来 对于01背包:把它想象成最大物品数为1的多重背包 对于完全背包:把它想象成最大物品数为m/w[i]的多重背包 对于多重背包:把它想象成...等等这本 ...

  7. C++ STL, set用法。 待更新zzzzz

    set集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值:另外,还 ...

  8. photon server (1)

    Photon是一套使用广泛的socket server引擎,服务端底层C++编写,客户端C#编写,跨多平台,收费,效率可观的一款引擎.实用上前有九城游戏(原魔兽世界代理),现在笔者发现多款腾讯旗下3D ...

  9. 关于openstack自动化安装的一点思考

    在openstack针对具体应用的解决方案已成型之后,按照官方文档安装openstack实际上就成了一个繁琐的事情. 我有一个设想,之前实施了一点,还未成功,但是也想记录下来.最终目标:使用U盘直接部 ...

  10. HTTP 2 VS HTTP 1.1

    提升H5应用加载速度的方式有很多,比如缓存.cdn加速.代码压缩合并和图片压缩等技术. 今天介绍的是HTTP 2.0