LeetCode 958. Check Completeness of a Binary Tree
原题链接在这里:https://leetcode.com/problems/check-completeness-of-a-binary-tree/
题目:
Given a binary tree, determine if it is a complete binary tree.
Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
Example 1:

Input: [1,2,3,4,5,6]
Output: true
Explanation: Every level before the last is full (ie. levels with node-values {1} and {2, 3}), and all nodes in the last level ({4, 5, 6}) are as far left as possible.
Example 2:

Input: [1,2,3,4,5,null,7]
Output: false
Explanation: The node with value 7 isn't as far left as possible.
Note:
- The tree will have between 1 and 100 nodes.
题解:
Perform level order traversal on the tree, if popped node is not null, then add its left and right child, no matter if they are null or not.
If the tree is complete, when first met null in the queue, then the rest should all be null. Otherwise, it is not complete.
Time Complexity: O(n).
Space: O(n).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isCompleteTree(TreeNode root) {
LinkedList<TreeNode> que = new LinkedList<TreeNode>();
que.add(root);
while(que.peek() != null){
TreeNode cur = que.poll();
que.add(cur.left);
que.add(cur.right);
} while(!que.isEmpty() && que.peek() == null){
que.poll();
} return que.isEmpty();
}
}
类似Complete Binary Tree Inserter.
LeetCode 958. Check Completeness of a Binary Tree的更多相关文章
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 【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 ...
- 958. Check Completeness of a Binary Tree
题目来源 题目来源 C++代码实现 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
- 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 ...
- [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 ...
- Leetcode958. Check Completeness of a Binary Tree二叉树的完全验证性
给定一个二叉树,确定它是否是一个完全二叉树. 百度百科中对完全二叉树的定义如下: 若设二叉树的深度为 h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集 ...
- 图论-完全二叉树判定-Check Completeness of a Binary Tree
2020-02-19 13:34:28 问题描述: 问题求解: 判定方式就是采用层序遍历,对于一个完全二叉树来说,访问每个非空节点之前都不能访问过null. public boolean isComp ...
- 【一天一道LeetCode】#104. Maximum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
随机推荐
- JUC-FutureTask
得到别的线程任务的返回值 import lombok.extern.slf4j.Slf4j; import java.util.concurrent.Callable; import java.uti ...
- 用es6 封装的对数组便捷操作的算法
/* * @Description: 对数组的基本操作 * @LastEditors: Please set LastEditors * @Date: 2019-04-26 12:00:19 * @L ...
- linux 流量监控利器:iftop
在类Unix系统中可以使用top查看系统资源.进程.内存占用等信息. 查看网络状态可以使用netstat.nmap等工具. 若要查看实时的网络流量,监控TCP/IP连接等,则可以使用iftop. if ...
- 2019 汇量科技java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.汇量科技等公司offer,岗位是Java后端开发,因为发展原因最终选择去了汇量科技,入职一年时间了,也成为了面 ...
- Java之路---Day03
2019-10-17-21:18:33 方法 定义格式: public static void 方法名称() { 方法体 } 完整格式: 修饰符 返回值类型 方法名称(参数类型 参数名称,... ...
- kali之HexorBase数据库破解
HexorBase 用户名密码连接数据库 暴力破解 点击底栏 Bruteforces Databases Servers , 然后会弹出一个新界面 Databases Bruteforces 新界面 ...
- kafka汇总
Kafka 1. kafka概念 kafka是一个高吞吐亮的.分布式.基于发布/订阅(也就是一对多)的消息系统,最初由Linkedln公司开发的,使用Scala语言编写的,目前是Apache的开源项目 ...
- LocalStorageUtils
对localStorage进行封装: var LocalStorageUtils = new function (){ if(window.localStorage==null){ throw new ...
- 英文FRAUNCE法国FRAUNCE单词
France Alternative forms Fraunce In Fraunce, the inhabitants of one city were driven out and forced ...
- springboot使用RestHighLevelClient7简单操作ElasticSearch7增删查改/索引创建
本次操作是在 Windows上安装ElasticSearch7 进行操作 导入依赖 <?xml version="1.0" encoding="UTF-8&qu ...