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. 终极二分查找--传说十个人写九个有bug

    之前写过一篇极为罗嗦的二分查找,非常得意地以为以后就可以避免踩坑了,但是今天才知道二分查找可以写的既简洁又鲁棒,唉!还是要多学习啊! 给一个按照从大到小的顺序排序好的数组a[]={1,2,3,4,7, ...

  2. js如何判断字符串是否进行过window.btoa()转码

    window.btoa()是基于Base64算法的.window.btoa()只能将ASCII字符进行转码 因此我们需要了解Base64的原理及主要特征:Base64的原理在这里就不多说了,网上很多讲 ...

  3. js_day14

  4. easyui-combobox默认值绑定

    $('#combox_role').combobox({ panelHeight: , url: '../../Handler/GetComboxItems.ashx?type=0', valueFi ...

  5. Javascript高级程序设计读书笔记(第六章)

    第6章  面向对象的程序设计 6.2 创建对象 创建某个类的实例,必须使用new操作符调用构造函数会经历以下四个步骤: 创建一个新对象: 将构造函数的作用域赋给新对象: 执行构造函数中的代码: 返回新 ...

  6. ASP.net MVC 向子视图传递数据

    使用 RenderPage 加载子视图 @RenderPage("~/Shared/Component/Dialog.cshtml", new { title = "He ...

  7. datatable列操作

    DataTable myDt =dt;  //删除列  myDt.Columns.Remove("minArea");  myDt.Columns.Remove("max ...

  8. 修改UITextfield的Placeholder字体的颜色

    - (void)viewDidLoad { [super viewDidLoad]; self.title=@"修改UITextField的placeholder字体颜色"; UI ...

  9. IntelliJ IDEA启动web项目时突然变慢的原因

    在使用IntelliJ IDEA开发web项目过程中,有两次项目启动非常慢,大约要200s的时间: 第一次忘记是怎么解决的,第二次出现后,我就直接重新下载了代码,然后部署,启动,时间有恢复正常,只用了 ...

  10. hadoop容灾能力测试

    实验简单来讲就是 1. put 一个600M文件,分散3个replica x 9个block 共18个blocks到4个datanode 2. 我关掉了两个datanode,使得大部分的block只在 ...