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. python 爬虫 selenium 与 chromedriver

    selenium  安装 pip install   selenium chromedriver  下载 https://npm.taobao.org/mirrors/chromedriver?spm ...

  2. mysql(insert + str_to_date)

    str_to_date(date_str, '%Y-%m-%d %H:%i:%s') 替换为 substring_index(date_str, '.', 1); str_to_date(date_s ...

  3. 南大ics-pa/PA0过程及感想

    实验教程地址:https://nju-projectn.github.io/ics-pa-gitbook/ics2022/index.html 一.Ubuntu安装 在清华大学镜像站下载了Ubuntu ...

  4. nodejs查询

    const tagModel = require("../mongodb"); 1 const mongoose = require("mongoose"); ...

  5. CSMA/CA多路复用、载波监听、冲突避免

    CSMA/CA是避免各站点之间数据传输冲突的算法,其特点是发送包的同时不能检测到信道上有无冲突,只能尽量"避免".例如,如果计算机A和计算机C同时给计算机B发送一个控制消息,它们将 ...

  6. ClickHouse 使用

    最近mysql报表数据太多,要转移数据到 clickHouse ,顺便学学该数据仓库的使用 中文文档:https://clickhouse.com/docs/zh/ B站学习视频 : https:// ...

  7. Windows 11安装etcd

    一.从官方网站找到Windows版的安装包下载 https://etcd.io/ 把etcd的压缩包解压到D:/soft/etcd文件夹下,首先运行etcd.exe,这是启动etcd服务的,接着就可以 ...

  8. Visual Studio 快速生成构造函数

    生成构造函数快速操作 - Visual Studio (Windows) | Microsoft Learn 键盘 按 (Ctrl+.) 触发"快速操作和重构"菜单. 鼠标 右键单 ...

  9. Qt-设置背景色

    https://blog.csdn.net/qq_43793182/article/details/121980724?ops_request_misc=&request_id=&bi ...

  10. Bat命令进行压缩X文件夹下的文件夹及文件

    输出日志 echo off for /d %%i in (D:\project101\trunk\x_client\xProject\Assets\AssetBundles\Android~\*.) ...