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. C#中定时任务被阻塞问题

    目录 解决一个C#中定时任务被阻塞问题 1.摘要 2.C#中定时任务的最简方法 3.定时任务阻塞现象 4.阻塞现象原因分析 5.问题解决 1.摘要 本文会介绍一个C#中最简单定时任务的使用方法,以及会 ...

  2. windows server 2012以上版本离线安装 net framework3.5 方法

    方法1. 通过服务管理器安装操作系统原镜像文件 准备windows系统镜像文件,解压windows server.iso文件到 D:\WindowsOS 在服务器管理器上添加.NET Framewor ...

  3. Windows下的挖矿木马查杀

    MS016小组(原创) 上一篇文章 简单讲了一下挖矿木马 大概流程  文章地址: https://www.cnblogs.com/ms016/articles/7978880.html 今天讲分析一个 ...

  4. wpf 解决画图模糊或抗锯齿以及文字模糊或抗锯齿问题

    解决方案中使用的.Net FrameWork版本:4.6.1 画图模糊或抗锯齿: 控件属性加入  SnapsToDevicePixels="True" 文字模糊或抗锯齿: 控件属性 ...

  5. Spring MVC文件上传下载

    Spring MVC文件上传下载 单文件上传 底层是使用Apache fileupload 组件完成上传,Spring MVC对这种方式进行封装. pom.xml <dependency> ...

  6. debug / support 的步骤 / 解决问题的步骤

    尽量避免去看代码, 而是去作为当前用户去测试各种场景, 肯定有的场景是有问题的, 有点场景是没有问题, 那有问题和没有问题之间, 就能知道为什么了.代码太纷繁芜杂了, 看不懂的, 出问题都是在某个或者 ...

  7. js扩展符号

    扩展数组:const arr23 = ['a', 'b', 'c']; console.log(...arr23); a b c扩展字符串: abc = 'abcdefghi'; console.lo ...

  8. Centos7 安装Mysql8 主从数据库

    前提条件 准备了两台虚拟机 mysql-master 192.168.30.199, mysql-slave 192.168.30.198 1:官网下载并安装Mysql8 1:安装mysql 1.1: ...

  9. https原理(四)双向实践(java客户端+tcp代理)

    本文采用客户端与服务端共用一个密钥对 1 将https代理服务器(三)实践中的mkcert p12分解为一个公钥一个私钥 mac@macdeMacBook mkcert % openssl pkcs1 ...

  10. 2021年RT-Thread开发者大会

    Time:2021-12-18,地点:大中华6楼喜来登酒店 主办方: RT-Thread:寓意实时线程,瑞赛德 世界有成千上万个 RTOS(Real-time operating system,实时操 ...