当前位置:网站首页>模拟卷Leetcode【普通】222. 完全二叉树的节点个数
模拟卷Leetcode【普通】222. 完全二叉树的节点个数
2022-07-29 05:39:00 【邂逅模拟卷】
汇总:模拟卷Leetcode 题解汇总
222. 完全二叉树的节点个数
给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。
示例 1:
输入:root = [1,2,3,4,5,6]
输出:6
示例 2:
输入:root = []
输出:0
示例 3:
输入:root = [1]
输出:1
提示:
树中节点的数目范围是[0, 5 * 104]
0 <= Node.val <= 5 * 104
题目数据保证输入的树是 完全二叉树
进阶:遍历树来统计节点是一种时间复杂度为 O(n) 的简单解决方案。你可以设计一个更快的算法吗?
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/count-complete-tree-nodes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
def Tree2List(root):
"""二叉树 -> 列表"""
if not root:return []
res = []
last = [root]
while last:
now = []
for node in last:
if node:
res.append(node.val)
now.append(node.left)
now.append(node.right)
else:
res.append(None)
last = now
while len(res)>1 and res[-1]==None: res.pop(-1)
return res
class Solution:
def countNodes(self, root: TreeNode) -> int:
return len(Tree2List(root))
def test(data_test):
s = Solution()
data = data_test # normal
# data = [List2Node(data_test[0])] # list转node
return s.getResult(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun,data in zip(data_test[0][1::],data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[],
]
for data_test in datas:
t0 = time.time()
print('-'*50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- 【干货备忘】50种Matplotlib科研论文绘图合集,含代码实现
- Teacher wangshuyao's notes on operations research 06 linear programming and simplex method (geometric significance)
- Invalid access control
- Embedding理解+代码
- Annotation
- 【论文阅读 | 冷冻电镜】RELION 4.0 中新的 subtomogram averaging 方法解读
- 吴恩达老师机器学习课程笔记 05 Octave教程
- 【笔记】The art of research - (讲好故事和论点)
- 吴恩达老师机器学习课程笔记 00 写在前面
- Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation
猜你喜欢

SQL developer graphical window to create database (tablespace and user)

联邦学习后门攻击总结(2019-2022)

矩阵分解与梯度下降

Ping principle

【论文阅读】TomoAlign: A novel approach to correcting sample motion and 3D CTF in CryoET

MySQL:当你CRUD时BufferPool中发生了什么?十张图就能说清楚

Ali gave several SQL messages and asked how many tree search operations need to be performed?

Let the computer run only one program setting

【讲座笔记】如何在稀烂的数据中做深度学习?

Condition 条件对象源码浅读
随机推荐
Unity探索地块通路设计分析 & 流程+代码具体实现
【解决方案】ERROR: lib/bridge_generated.dart:837:9: Error: The parameter ‘ptr‘ of the method ‘FlutterRustB
Teacher Wu Enda's machine learning course notes 00 are written in the front
Understanding of access, hybrid and trunk modes
The core of openresty and cosocket
Apisik health check test
IO流 - File - properties
软件定义边界SDP
DM数据守护集群搭建
说一下 TCP/IP 协议?以及每层的作用?
Not so simple singleton mode
Teacher wangshuyao's notes on operations research 03 KKT theorem
Talk about tcp/ip protocol? And the role of each layer?
Biased lock, lightweight lock test tool class level related commands
CNN-卷积神经网络
Enterprise manager cannot connect to the database instance in Oracle10g solution
会话推荐中的价格偏好和兴趣偏好共同建模-论文泛读
NLP word segmentation
API for using the new date class of instant
Teacher wangshuyao's notes on operations research 05 linear programming and simplex method (concept, modeling, standard type)