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. selenium------关于switch_to的用法场景

    基于python3的语法,driver.switch_to_alert()的表达会出现中划线,因此需要把后面的下划线改为点.一.目前接触到的switch_to的用法包括以下几种:1. 切换到制定的wi ...

  2. 在百度云服务器上部署Django网站的经历

    前言: 前段时间,利用Django为单位制作了一个小型的内部考勤系统,本想放到单位内部的服务器上,考虑到运行的稳定.安全防护等问题,最终决定把网站部署到百度云服务器上,事先也在网上查找了一些资料,但过 ...

  3. (0313) ICer,root 权限密码

    2020

  4. 环保行业ERP主要的几大治理区域?

    环保行业是指在国民经济结构中,以防治环境污染.改善生态环境.保护自然资源为目的而进行的技术产品开发.商业流通.资源利用.信息服务.工程承包等活动的总称. 哲讯环保ERP行业产业链的上游主要是钢铁.有色 ...

  5. 用telnet远程连接linux系统

    环境,centos7.6 通过telnet进行远程登录的方法如下: 1.安装telnet,telnet-server,xinetd 检测是否安装telnet rpm -qa | grep telnet ...

  6. 赤菟V307与Matlab的串口通信

    赤菟V307与Matlab的串口通信 赤菟V307(CH32V307)是一款RISC-V内核的MCU,搭载的是沁恒自研RISC-V内核青稞V4F,最高主频144MHz,支持单精度浮点运算(FPU). ...

  7. 基于Mybatis Plus的一种查询条件构建方案

    ! 重要: 遐(瞎)想的思路, 希望各位多多建议 record为jdk17写法, 使用class也不会有问题 背景 身为资深程序员, 上班最重要的事当然是增删改查(bushi). 比如今天, 组长甩给 ...

  8. P5731 蛇形方阵

    P5731 [深基5.习6]蛇形方阵 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) //为什么用动态二维数组 --->To play to user's input, but ...

  9. vue3 深度选择器 scss用法

    使用 :deep() 替换 ::v-deep .carousel { // Vue 2.0 写法 // ::v-deep .carousel-btn.prev { // left: 270px; // ...

  10. zabbix中文显示乱码解决

    问题zabbix使用中文显示,"监测-->图形"查看资源使用情况时会有乱码 解决问题1.修改配置文件(文件位置:$zabbix_path/include/defines.in ...