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. jQuery中ajax的使用和缓存问题解决 $getjson 与$get都会被IE缓存

    原文地址:http://www.cnblogs.com/fullhouse/archive/2012/01/17/2324842.html 1:GET访问 浏览器 认为 是等幂的就是 一个相同的URL ...

  2. 掌握 Java 8 Lambda 表达式

    Lambda 表达式 是 Java8 中最重要的功能之一.使用 Lambda 表达式 可以替代只有一个函数的接口实现,告别匿名内部类,代码看起来更简洁易懂.Lambda 表达式 同时还提升了对 集合 ...

  3. pyqt之倒计时例子

    from PyQt4.Qt import *from PyQt4.QtCore import *from PyQt4.QtGui import *import sysdef main():    a= ...

  4. iphone UIScrollView缩放

    allImageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)]; allImageScrol ...

  5. mysql 1449 : The user specified as a definer (&#39;root&#39;@&#39;%&#39;) does not exist 解决方法

    权限问题,授权 给 root  全部sql 权限 mysql> grant all privileges on *.* to root@"%" identified by & ...

  6. 使用HashMap对象传递url參数有用工具类

    代码例如以下: package com.yanek.util; import java.util.ArrayList; import java.util.Collections; import jav ...

  7. HashSet的分析(转)

    一.  HashSet概述: HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持.它不保证set 的迭代顺序:特别是它不保证该顺序恒久不变.此类允许使用null元素. 二.  ...

  8. c#基础: NetWorkStream类的主要属性

    一.网络流 1.    最常用的方法  Read()  Write()    Flush() NetworkStream netStream = new NetworkStream(mesock); ...

  9. SuperSocket学习笔记(一)

    这是根据我自己学习的经历整理出来的,如有不对之处,还请多多指教! SuperSocket源码下载 SuperSocket文档 安装并启动Telnet 学习方法: QuickStrart + 文档 参考 ...

  10. oracle包概述(一)【weber出品】

    一.PL/SQL包概述 1. 什么是PL/SQL包: 相关组件的组合:PL/SQL类型.变量,数据结构,和表达式.子程序: 过程和函数 2. 包的组成部分: 由两部分组成: 包头 包体 3. 包的优点 ...