[LeetCode] 完全二叉树的节点个数
题目链接: https://leetcode-cn.com/problems/count-complete-tree-nodes
难度:中等
通过率:57.4%
题目描述:
给出一个 完全二叉树 ,求出该树的节点个数。
说明:
完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第h 层,则该层包含 1~ 2h 个节点。
示例:
输入:
1
/ \
2 3
/ \ /
4 5 6
输出: 6
思路:
暴力,每个节点都访问一下,看有多少个
时间复杂度:\(O(n)\)
class Solution:
def countNodes(self, root: TreeNode) -> int:
if not root: return 0
return 1 + self.countNodes(root.left) + self.countNodes(root.right)
这样不是完全二叉树也可以计算,但是没有用到完全二叉树的性质!
我们可以通过运用性质,完全二叉树的节点个数等于 \(2^{h} - 1\),
所以有,时间复杂度:\(O(log(n)^2)\)
class Solution:
def countNodes(self, root: TreeNode) -> int:
if not root: return 0
left_height = 0
left_node = root
right_height = 0
right_node = root
while left_node:
left_node = left_node.left
left_height += 1
while right_node:
right_node = right_node.right
right_height += 1
if left_height == right_height:
return pow(2,left_height) - 1
return 1 + self.countNodes(root.left) + self.countNodes(root.right)
[LeetCode] 完全二叉树的节点个数的更多相关文章
- Leetcode 222.完全二叉树的节点个数
完全二叉树的节点个数 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最 ...
- Java实现 LeetCode 222 完全二叉树的节点个数
222. 完全二叉树的节点个数 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集 ...
- leetcode-222完全二叉树的节点个数
题目 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置. ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- LeetCode 222.完全二叉树的节点个数(C++)
给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底 ...
- Leetcode 222:完全二叉树的节点个数
题目 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置. ...
- LeetCode 222. 完全二叉树的节点个数(Count Complete Tree Nodes)
题目描述 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位 ...
- [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
随机推荐
- codevs 1231 最优布线问题 x(find函数要从娃娃抓起系列)
题目描述 Description 学校需要将n台计算机连接起来,不同的2台计算机之间的连接费用可能是不同的.为了节省费用,我们考虑采用间接数据传输结束,就是一 ...
- 微信支付(公众号)爬坑记,包含 total_fee 失败和 JSAPI 签名验证失败等等
做商城类网站不免会需要做支付功能,目前在中国大陆通用的做法就是使用支付宝支付和微信支付,上一篇博文已经讲个支付宝支付. 这篇文章来讲一讲微信支付,微信支付的方式有很多种,本文主要讲 JSAPI 支付的 ...
- sql 建立索引之前计算区分度
select cutomer_id,title,content from product_comment where audit_status=1 and product_id=1 and produ ...
- Spring Boot使用阿里云证书启用HTTPS
1.到阿里云下载证书页面下载证书 2.根据页面内容,可以使用2种证书:PFX JKS 把对应证书放到src/main/resources目录下 在application.properties文件中加入 ...
- C++入门经典-例2.10-控制输出精确度
1:代码如下: // 2.10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...
- Spring Boot中使用 Thymeleaf
目录 1.pom.xml引入thymeleaf 2.关闭缓存application.properties 3.编写Controller类 4.模板html 5.运行结果 1.pom.xml引入thym ...
- 191024DjangoORM之单表操作
一.ORM基础 ORM:object relation mapping 对象关系映射表 1.配置连接MySQL settings.py:将默认配置删除,加入以下配置 DATABASES = { 'de ...
- How to correctly use preventDefault(), stopPropagation(), or return false; on events
How to correctly use preventDefault(), stopPropagation(), or return false; on events I’m sure this h ...
- nginx不记录指定文件类型的日志
1.指定记录文件日志记录的内容. vim /usr/local/nginx/conf/nginx.conf如下部分: log_format dd '$remote_addr $http_x_forwa ...
- 因修改/etc/ssh权限导致的ssh不能连接异常解决方法
因修改/etc/ssh权限导致的ssh不能连接异常解决方法 现象: $ssh XXX@192.168.5.21 出现以下问题 Read from socket failed: Connection r ...