静态建树判一下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. DELPHI 动态 创建和释放 多个 EDIT 控件

    unit Unit1;   interface   uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, ...

  2. Hibernate学习---第十二节:Hibernate之锁机制&乐观锁实现

    1.悲观锁 它指的是对数据被外界修改保持保守态度,因些,在整个数据处理过程中,将数据牌锁定状态.悲观锁的实现,往往依靠数据库提供的锁机制(也只有数据库层的锁机制才能保证数据访问的排他性,否则,即使在本 ...

  3. WebElement接口获取值

    通过WebElement接口获取值 size 获取元素的尺寸 text 获取元素的文本 get_attribute(name) 获取属性值 location 获取元素坐标,先找到要获取的元素,再调用该 ...

  4. python中的排序函数

    1.sort() list类型有一个自带的排序函数sort() list.sort(cmp=None, key=None, reverse=False) 参数说明: (1)  cmp参数 cmp接受一 ...

  5. Struts2 - 配置文件中result 节点详解

    每个 action 方法都将返回一个 String 类型的值, Struts 将根据这个值来决定响应什么结果. 每个 action 声明都必须包含有数量足够多的 result 元素, 每个 resul ...

  6. 九省联考2018 D1T1 一双木棋

    Alice和Bob轮流在n*m的棋盘上放棋子 a[i][j]表示Alice放在这的收益,b[i][j]表示Bob放在这的收益 一个地方没有棋子且它的左边上边都有棋子才能放棋子,边界外视为有一圈棋子 n ...

  7. 【LeetCode】019. Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  8. 基于无锁的C#并发队列实现

    最近开始学习无锁编程,和传统的基于Lock的算法相比,无锁编程具有其独特的优点,Angel Lucifer的关于无锁编程一文对此有详细的描述. 无锁编程的目标是在不使用Lock的前提下保证并发过程中共 ...

  9. NorthSJ项目零碎知识点

    1 div显示纵向滚动条: <div style="overflow-y:auto;overflow-x:hidden;"></div> 2 FineUI的 ...

  10. 【转】Pro Android学习笔记(十一):了解Intent(中)

    Intent的构成 Intent可以带有action,data(由URI表达),extra data(key/value map,键值对),指定的类名(成为component name).一个inte ...