题目如下:

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:

  1. Input: [1,2,3,4,5,6]
  2. Output: true
  3. 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:

  1. Input: [1,2,3,4,5,null,7]
  2. Output: false
  3. 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)是否相等即可。

代码如下:

  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7.  
  8. class Solution(object):
  9. number = []
  10. maxNum = 0
  11. def traverse(self,node,seq):
  12. self.maxNum = max(self.maxNum,seq)
  13. self.number.append(seq)
  14. if node.left != None:
  15. self.traverse(node.left,seq*2)
  16. if node.right != None:
  17. self.traverse(node.right,seq*2+1)
  18. def isCompleteTree(self, root):
  19. """
  20. :type root: TreeNode
  21. :rtype: bool
  22. """
  23. self.number = []
  24. self.maxNum = 0
  25. self.traverse(root,1)
  26. 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. python 中字符串中含变量方法

    1. 简单粗鲁的字符串拼接 1 name = "abc" 2 age = 25 3 info = "the name is "+name +"\nth ...

  2. Oracle 19C的下载和安装部署

    1.官网下载zip包. 2.解压到/usr/local/oracle 目录. 3.创建用户和用户组 /usr/sbin/useradd -u oracle //用户组oracle /usr/sbin/ ...

  3. PHP filter_list() 函数

    定义和用法 filter_list() 函数返回包含所有得到支持的过滤器的一个数组. 语法 filter_list() 提示和注释 注释:该函数的结果不是过滤器 ID,而是过滤器名称.请使用 filt ...

  4. POJ 2955 Brackets (区间dp入门)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  5. POJ 3126 Prime Path (bfs+欧拉线性素数筛)

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  6. <Jmeter入门不放弃>之<2.常用功能>

    大家这里参考学习的时候,我就不在这里配截图了,因为需要你打开工具根据文档自己去找,才有印象,大家一定要启动JMeter!跟着理解操作 一.测试计划 用来描述一个性能测试,所有内容都是基于这个计划,这谁 ...

  7. JMeter ServerAgent服务器资源监控插件

    本文介绍对Linux服务器的服务进行压测时,使用jmeter serverAgent插件监控服务器资源. 1.插件准备 所需插件: JMeterPlugins-Extras.jar JMeterPlu ...

  8. Java 从入门到进阶之路(十七)

    在之前的文章我们介绍了一下 Java 中类的内部类,本章我们来看一下 Java 中的正则表达式. 在任何一种语言中,都绕不开正则表达式,而且大部分语言的正则表达式都有预定义的字符集,且预定义的字符集也 ...

  9. 基于nodejs的一个实时markdown转html工具小程序

    1.版本一 - 1.1`npm install marked --save` 安装markdwon转html的包.- 1.2 使用watchFile监视 markdown文件 /** * Create ...

  10. C++构造函数异常(一)

    C++ 构造函数的异常是一个比较难缠的问题,很多时候,我们可能不去考虑这些问题,如果被问到,有人可能会说使用RAII管理资源. 但你真的考虑过如果构造函数失败了,到底会发生什么吗,前面构造成功的成员. ...