Check if a given binary tree is completed. A complete binary tree is one in which every level of the binary tree is completely filled except possibly the last level. Furthermore, all nodes are as far left as possible.

Examples

5

/    \

3        8

/   \

1      4

is completed.

5

/    \

3        8

/   \        \

1      4        11

is not completed.

Corner Cases

  • What if the binary tree is null? Return true in this case.

How is the binary tree represented?

We use the level order traversal sequence with a special symbol "#" denoting the null node.

For Example:

The sequence [1, 2, 3, #, #, 4] represents the following binary tree:

1

/   \

2     3

/

4

/**
* public class TreeNode {
* public int key;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int key) {
* this.key = key;
* }
* }
*/
public class Solution {
public boolean isCompleted(TreeNode root) {
// Write your solution here
if (root==null){
return true;
}
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root); int isLeaf = 0;
while(!q.isEmpty()){
Queue<TreeNode> nextQ = new LinkedList<TreeNode>();
int size = q.size();
for(int i=0; i<size; i++){
TreeNode curNode = q.poll();
if(curNode.left==null && curNode.right!=null){
return false;
}
if(curNode.left!=null){
if(isLeaf==1){
return false;
}
nextQ.offer(curNode.left);
}
if(curNode.right!=null){
if(isLeaf==1){
return false;
}
nextQ.offer(curNode.right);
}
if(curNode.left==null || curNode.right==null){
isLeaf=1;
}
}
q = nextQ;
}
return true; }
}

Check If Binary Tree Is Completed的更多相关文章

  1. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  2. Check whether a given Binary Tree is Complete or not 解答

    Question A complete binary tree is a binary tree in which every level, except possibly the last, is ...

  3. [Swift]LeetCode958. 二叉树的完全性检验 | Check Completeness of a Binary Tree

    Given a binary tree, determine if it is a complete binary tree. Definition of a complete binary tree ...

  4. leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes

    完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...

  5. [Algorithm] Check if a binary tree is binary search tree or not

    What is Binary Search Tree (BST) A binary tree in which for each node, value of all the nodes in lef ...

  6. 115th LeetCode Weekly Contest Check Completeness of a Binary Tree

    Given a binary tree, determine if it is a complete binary tree. Definition of a complete binary tree ...

  7. Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST

    题目原文: Given a binary tree where each 

  8. LeetCode 958. Check Completeness of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/check-completeness-of-a-binary-tree/ 题目: Given a binary tree, ...

  9. 958. Check Completeness of a Binary Tree

    题目来源 题目来源 C++代码实现 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...

  10. 【leetcode】958. Check Completeness of a Binary Tree

    题目如下: Given a binary tree, determine if it is a complete binary tree. Definition of a complete binar ...

随机推荐

  1. ubuntu16.04编译LAPACK3.7.1出错

    NEP: Testing Nonsymmetric Eigenvalue Problem routines ./EIG/xeigtstz < nep.in > znep.out 2> ...

  2. desginer启动就直接卡死

    博主经验: 请不要开有道词典    请不要开有道词典         请不要开有道词典

  3. Go--生成excel表格,读取excel表格数据

    先下载第三方依赖包: go get -u github.com/xuri/excelize/v2 代码: package main import ( "fmt" "git ...

  4. mqtt抓包

      mqtt消息抓包 账号.密码.imappclientId 或者imwebclientId校验成功后跟imserver连接成功 部分交互:先获取到uid,再根据uid拿到其他用户信息,且部分信息通过 ...

  5. k8s如何配置secret保存harbor仓库账号密码、pod中怎么使用harbor仓库镜像

    转载: https://blog.csdn.net/MssGuo/article/details/127312239

  6. Mac 启动转换助理 安装 win10

    Mac 启动转换助理 安装 win10 Mac的处理器 是Inter芯 才有该功能 打开启动转换助理/Bootcamp 设置分区大小 win10安装完成后 在左侧的此电脑中找到咱们的Boot Camp ...

  7. GeoServer 发布PostGIS数据库中的栅格数据

    1.导入栅格数据 进入PostgreSQL\bin目录,利用raster2pgsql工具导入栅格数据,具体命令如下所示: <!-- 分块,切片存储到PostGIS数据库中 --> rast ...

  8. bsub opts

    Options - IBM Documentation List of options for the bsub command. -aSpecifies one or more applicatio ...

  9. nginx日志按日期存储

    http {     include       mime.types;     default_type  application/octet-stream;     map $time_iso86 ...

  10. 戴尔n4110在win7下无法使用virtualbox的解决方法(应该对win7都有用)

    正文 因为已经学了一段时间的汇编了嘛,想着就拿单独一台机器出来学汇编好了,刚好趁着天气降温回学校拿被子的机会把笔记本也拿出来了,然后我装上了virtual box,把编译好的文件写到虚拟盘中,打开就直 ...