题目如下:

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:

  1. The tree will have between 1 and 100 nodes.

解题思路:完全二叉树有这个一个定律:完全二叉树中任一节点编号n,则其左子树为2n,右子树为2n+1。所以,只要遍历一遍二叉树,记根节点的编号为1,依次记录每个遍历过的节点的编号,同时记录编号的最大值MAX。最后判断所有遍历过的节点的编号的和与sum(1~MAX)是否相等即可。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
number = []
maxNum = 0
def traverse(self,node,seq):
self.maxNum = max(self.maxNum,seq)
self.number.append(seq)
if node.left != None:
self.traverse(node.left,seq*2)
if node.right != None:
self.traverse(node.right,seq*2+1)
def isCompleteTree(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
self.number = []
self.maxNum = 0
self.traverse(root,1)
return (self.maxNum+1)*self.maxNum/2 == sum(self.number)

【leetcode】958. Check Completeness of a Binary Tree的更多相关文章

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

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

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

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

  3. LeetCode 958. Check Completeness of a Binary Tree

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

  4. 【LeetCode】637. Average of Levels in Binary Tree 解题报告(Python)

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

  5. 【LeetCode】Verify Preorder Serialization of a Binary Tree(331)

    1. Description One way to serialize a binary tree is to use pre-order traversal. When we encounter a ...

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

  7. 【leetcode】637. Average of Levels in Binary Tree

    原题 Given a non-empty binary tree, return the average value of the nodes on each level in the form of ...

  8. 958. Check Completeness of a Binary Tree

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

  9. 【leetcode】1104. Path In Zigzag Labelled Binary Tree

    题目如下: In an infinite binary tree where every node has two children, the nodes are labelled in row or ...

随机推荐

  1. css 多行省略号兼容移动端

    浏览器兼容css样式 -webkit-line-clamp: ; display: -webkit-box; overflow: hidden; text-overflow: ellipsis; te ...

  2. elasticsearch 6.x 安装search guard

    前言 es之前版本一直无用户验证功能,不过官方有提供一x-pack,但是问题是付费.在es的6.3.2版本中,已经集成了x-pack,虽然es团队已经对x-pack开源,但是在该版本中如果需要使用到安 ...

  3. POJ 2299 Ultra-QuickSort (树状数组+离散化 求逆序数)

    In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...

  4. eureka学习(二)

    eureka服务端创建好后,现在我们让eureka客户端(也就是服务提供者)注册到eureka上去. 首先加入依赖包: <!--将微服务provider注册到eureka--> <d ...

  5. Promise、async、await 异步解决方案

    参考: https://www.cnblogs.com/CandyManPing/p/9384104.html  或  https://www.jianshu.com/p/fe0159f8beb4(推 ...

  6. Dart 和 Flutter 使用json_annotation和json_serializable来处理json数据教程

    在学习fultter的时候突然想到如何去处理从服务器获取的json或者将app中的对象数据转换成json上传给服务器 于是研究一下dart对json数据的处理 首先需要依赖下面的第三方库(这里要强调下 ...

  7. 82、TensorFlow教你如何构造卷积层

    ''' Created on 2017年4月22日 @author: weizhen ''' import tensorflow as tf #通过tf.get_variable的方式创建过滤器的权重 ...

  8. winform中的小技巧【自用】

    一.C#在WinForm中怎样让多行TEXTBOX的换行 今天做项目,有一段提示文字需要弹出来,由于太长,我就想能不能让它换行.然后就百度了一下,嘿嘿,方法很好用哦. 原文链接:https://blo ...

  9. 研究一下phpspider

    官方文档 1.下载 官方github下载地址: https://github.com/owner888/phpspider 下载地址可能无法访问,这里提供一个网盘下载地址: 链接: https://p ...

  10. 08 java代码块的概述和分类

    08.01_面向对象(代码块的概述和分类) A:代码块概述 在Java中,使用{}括起来的代码被称为代码块. B:代码块分类 根据其位置和声明的不同,可以分为局部代码块,构造代码块,静态代码块,同步代 ...