### 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) {
if (node.right != null) {
flag = true;
} else {
flag = false;
}
}
}
current = next;
} return true;
}
}

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. 958. Check Completeness of a Binary Tree

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

7. Binary Tree Zigzag Level Order Traversal 解答

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

8. Binary Tree Level Order Traversal II 解答

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

9. Data Structure Binary Tree: Check if a given Binary Tree is SumTree

http://www.geeksforgeeks.org/check-if-a-given-binary-tree-is-sumtree/ #include <iostream> #inc ...

## 随机推荐

1. 在HTML页面获取当前系统时间

<script language="javascript"> function getCurDate() { var d = new Date(); var week; ...

2. Android关于实现EditText中加多行下划线的的一种方法

1. 重写EditText public class LinedEditText extends EditText { private Paint linePaint; private float m ...

3. Netty4.x分析

官网定义: netty是一个异步.事件驱动的网络应用框架,用于快速开发可维护的.高性能的服务端和客户端程序. 原理分析  Architecture Overview 网络模型:netty采用了Reac ...

4. js和HTML结合（补充知识：如何防止文件缓存的js代码）

来自<javascript高级程序设计 第三版:作者Nicholas C. Zakas>的学习笔记(二) 使用html标签<script>可以把js嵌入到html页面中,让脚本 ...

5. thinkphp 常用的查询

php 常用的数据库查询方式: //根据where 条件查询,使用select()方法 访问:http://localhost/thinkphp2/index.php/Machine/search_i ...

6. 内Cool超人

内Cool超人 经过一年时间看到asp.net mvc一直被受微软开发团队的注重.与之相比的silverlight我感觉到有点力不从心.除去silverlight第一次运行要安装Runtime不说,产 ...

7. win7下安装sdks

原文及更多内容:http://yysource.sourceforge.net/?p=103 下载和安装 Windows 调试工具 http://msdn.microsoft.com/zh-CN/wi ...

8. 5.Smart使用内置函数或者自定义函数

1.使用内置函数 例如使用date函数 {"Y-m-d"|date:\$time}格式{第一个参数|方法:第二个参数:第三个参数}即可转换成 2016-07-19  2.使用resi ...