Question

complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

Solution 1 -- BFS

Using BFS and a flag.

 public class Solution {
public boolean checkCompleteBinaryTree(TreeNode root) {
// BFS
// flag whether means previous node has both children
boolean flag = true;
List<TreeNode> current = new ArrayList<TreeNode>();
List<TreeNode> next;
current.add(root); while (current.size() > 0) {
next = new ArrayList<TreeNode>();
for (TreeNode node : current) {
if (!flag && (node.left != null || node.right != null))
return false;
if (node.left == null && node.right != null)
return false;
if (node.left != null) {
next.add(node.left);
if (node.right != null) {
next.add(node.right)
flag = true;
} else {
flag = false;
}
}
}
current = next;
} return true;
}
}

Solution 2 -- Recursive

Using recursion

Check whether a given Binary Tree is Complete or not 解答的更多相关文章

  1. leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes

    完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...

  2. [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 ...

  3. 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 ...

  4. LeetCode 958. Check Completeness of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/check-completeness-of-a-binary-tree/ 题目: Given a binary tree, ...

  5. 【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 ...

  6. 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  7. 958. Check Completeness of a Binary Tree

    题目来源 题目来源 C++代码实现 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...

  8. Binary Tree Zigzag Level Order Traversal 解答

    Question Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, fro ...

  9. Binary Tree Level Order Traversal II 解答

    Question Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, ...

随机推荐

  1. URAL 1658

    题目大意:求出T个最小的满足各个位的和为S1,平方和为S2的数.按顺序输出.数的位数大于100或者不存在这样一个数时,输出:No solution. KB     64bit IO Format:%I ...

  2. spark1.3的部署

    1.下载源码,根据自己的环境编译,我这里下载的是spark1.3版本 本人采用sbt编译, SPARK_HADOOP_VERSION=2.5.2 SPARK_YARN=ture sbt/sbt ass ...

  3. Makefile学习(二)条件判断和内嵌函数

    第七章:Makefile的条件执行 条件语句可是是两个不同的变量.或者变量和常量值的比较: 7.1例子: 对变量“CC”进行判断,其值如果是“gcc ”那么在程序连接时使用库“libgnu.so”或者 ...

  4. [Angular 2] WebStorm - Managing Imports

    Some tips for import libaray by using webstorm: // Alt + Enter --> Auto Import // Ctrl + Alt + o ...

  5. jquery压缩图片插件

    imageCompress 只有图片压缩功能,比较简单jquery.imageCompress.js 使用说明: el:为上传框 quality:压缩图片质量,单位为% onloadStart:读取图 ...

  6. Android开源代码解读-基于SackOfViewAdapter类实现类似状态通知栏的布局

    一般来说,ListView的列表项都会采用相同的布局,只是填充的内容不同而已,这种情况下,Android提供了convertView帮我们缓存列表项,达到循环利用的目的,开发者也会使用ViewHold ...

  7. 使用hbase的api创建表时出现的异常

    /usr/lib/jvm/java-7-openjdk-amd64/bin/java -Didea.launcher.port=7538 -Didea.launcher.bin.path=/usr/l ...

  8. Asp.net实现在线人数统计功能代码实例

    application最经典的一个方法:统计在线人数,这需要借助于我们的全局应用程序类来对登录的用户信息进行统计: 以下是代码片段:    void application_start(object ...

  9. Javascript实现表格行排序

    网站开发中凡是用到表格来展示数据的,往往都要根据某个列来对行排序,下面是我从书上看到的一个行排序例子,看过后受益匪浅,故分享出来. 直接献上完整代码: <!doctype html> &l ...

  10. Installation error: INSTALL_CANCELED_BY_USER

    这个错误,从字面上理解,就是已经安装的时候被用户取消了. 我出现这个错误的时候,是手机连接在电脑上的. 经过网上搜索,确认这是安装apk到手机时,被手机取消了安装操作. 1. 确保手机处于开发者模式. ...