【leetcode】958. Check Completeness of a Binary Tree
题目如下:
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:
- 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的更多相关文章
- 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- LeetCode 958. Check Completeness of a Binary Tree
原题链接在这里:https://leetcode.com/problems/check-completeness-of-a-binary-tree/ 题目: Given a binary tree, ...
- 【LeetCode】637. Average of Levels in Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 【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 ...
- 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 ...
- 【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 ...
- 958. Check Completeness of a Binary Tree
题目来源 题目来源 C++代码实现 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
- 【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 ...
随机推荐
- c++后台开发面试常见知识点总结(五)场景设计
搜索引擎的实现,会用到哪些重要的数据结构 设计实现一个HTTP代理服务器 / web服务器 / FTP服务器/ 设计实现cache缓存web服务器的网页访问记录 把一个文件快速下发到100w个服务器 ...
- python学习--第三天 粗略介绍人脸识别
首先安装opencv 在安装opencv过程中遇到一些错误(百度解决) 直接贴代码吧,讲师略讲了一下,体会不深,以后有机会深入学习,再详细介绍解释吧 人脸识别训练集应该可以网上下载吧,都是开源的 im ...
- 怎么让小白理解intel处理器(CPU)的分类
https://www.zhihu.com/question/32669957 目录 如何选购台式机CPU? 1. 英特尔处理器简介(本文) 1.1 聊聊Intel Tick-Tock 2. AMD处 ...
- QTcpSocket发送结构体
我需要发送的结构体 struct NetDataHeader_t { int nDataType; int nDataSize; }; struct NetDataBase_t { NetDataHe ...
- jq动画效果慢慢滚动到固定位置
function contentTop(top){ $('body,html').animate({ scrollTop: top }, 500 ); } 获取元素top传入即可 contentTop ...
- ARM 是什么
ARM Advanced RISC Machines. RISC 就是reduced instruction set computer 精简指令集计算机DSP digtal signal Proces ...
- leetcode中的一些二分搜索树
235(最近公共祖先) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left ...
- 有关JWT(Json Web Token)的那些事
用户认证是计算机安全领域一个永恒的热点话题,然而你会发现,开发者很少讨论有关Json Web Token的话题,其实使用Json Web Token集成到API身份验证机制中是容易,本文给大家普及基础 ...
- centos 安装 Lamp(Linux + Apache + PHP) 并安装 phpmyadmin
来源:http://www.laozhe.net/302.html 一般情况下,安装的都是最新的正式版,除非你有特殊需求,要安装指定的版本,本文暂不讨论.从最基础的开始,一点点完成一个可用的 Linu ...
- javaScript的事件冒泡事件捕获
(1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. IE 5.5: div -> body -> document IE 6.0: div ...