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 ...
随机推荐
- Delphi Mercadopago支付【支持支持获取账户信息和余额、创建商店,商店查询、创建二维码、二维码查询、创建订单、订单查询、订单退款等功能】
作者QQ:(648437169) 点击下载➨Delphi Mercadopago支付 [Delphi Mercadopago支付]支持 支持支持获取账户信息和余额.创建商店,商店查询.创建二维码.二维 ...
- head first c# -- 第七章 (接口与抽象类)
接口的作用: 例子:有鸡,鸭,牛,羊4个类,还有farmer类: farmer.feed(obj obj) { obj.eat() } // 没有接口: farmer.feedChicken(Chic ...
- 英语insuraunce保险
中文名:保险 外文名:insurance或insuraunce 类型:保障机制,商业行为 作用:资金融通.损失补偿等 原则:分摊.代位.大数法则等原则 性质:契约经济关系 意义:市场经济条件下风险管理 ...
- sublime配置python环境及快捷键
sublime配置python环境 参考链接:https://blog.csdn.net/VertigozZ/article/details/54574006 快捷键的配置:https://www.c ...
- 【题解】Luogu P5283 [十二省联考2019]异或粽子
原题传送门 看见一段的异或和不难想到要做异或前缀和\(s\) 我们便将问题转化成:给定\(n\)个数,求异或值最靠前的\(k\)对之和 我们珂以建一个可持久化01trie,这样我们就珂以求出每个值\( ...
- sqlserver 2005 数据库的差异备份与还原
找到一个可靠的步骤,点开链接:http://blog.csdn.net/kevindr/article/details/22154323
- 2019 迅雷java面试笔试题 (含面试题解析)
本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.迅雷等公司offer,岗位是Java后端开发,最终选择去了迅雷. 面试了很多家公司,感觉大部分公司考察的点都差不多 ...
- (转载) @ConfigurationProperties 注解使用姿势,这一篇就够了
SpringBoot中的@ConfigurationProperties 传送门: http://www.hellojava.com/a/82613.html
- 【WPF】EntityframeworkCore Update注意事项
The instance of entity type 'Book' cannot be tracked because another instance with the same key valu ...
- Python学习日记(二十八) hashlib模块、configparse模块、logging模块
hashlib模块 主要提供字符加密算法功能,如md5.sha1.sha224.sha512.sha384等,这里的加密算法称为摘要算法.什么是摘要算法?它又称为哈希算法.散列算法,它通过一个函数把任 ...