当前位置:网站首页>Simulation volume leetcode [normal] 222. number of nodes of complete binary tree
Simulation volume leetcode [normal] 222. number of nodes of complete binary tree
2022-07-29 06:56:00 【Encounter simulation volume】
Summary : Simulation volume Leetcode Summary of questions
222. The number of nodes in a complete binary tree
Here's a tree for you Perfect binary tree The root node root , Find out the number of nodes of the tree .
Perfect binary tree Is defined as follows : In a complete binary tree , Except that the lowest node may not be full , The number of nodes in each layer reaches the maximum , And the nodes of the lowest layer are concentrated in the leftmost positions of the layer . If the bottom layer is No h layer , Then the layer contains 1~ 2h Nodes .
Example 1:
Input :root = [1,2,3,4,5,6]
Output :6
Example 2:
Input :root = []
Output :0
Example 3:
Input :root = [1]
Output :1
Tips :
The number of nodes in the tree ranges from [0, 5 * 104]
0 <= Node.val <= 5 * 104
The topic data ensures that the input tree is Perfect binary tree
Advanced : Traversing the tree to count nodes is a method with time complexity of O(n) A simple solution . Can you design a faster algorithm ?
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/count-complete-tree-nodes
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
from leetcode_python.utils import *
def Tree2List(root):
""" Binary tree -> list """
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 turn 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')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- 【技能积累】presentation实用技巧积累,常用句式
- 竣达技术 | 适用于”日月元”品牌UPS微信云监控卡
- 王树尧老师运筹学课程笔记 05 线性规划与单纯形法(概念、建模、标准型)
- Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation
- 王树尧老师运筹学课程笔记 08 线性规划与单纯形法(单纯形法)
- DM数据守护集群搭建
- 数仓建模,什么是宽表?如何设计?好处与不足
- Best example of amortized cost
- 【笔记】The art of research(明白问题的重要性)
- 关于SQL Server语句入门级应用阶段性学习——找工作必备(一)
猜你喜欢

vscode通过remotessh结合xdebug远程调试php解决方案

Unity探索地块通路设计分析 & 流程+代码具体实现

【冷冻电镜|论文阅读】A feature-guided, focused 3D signal permutation method for subtomogram averaging

ECCV 2022丨轻量级模型架ParC-Net 力压苹果MobileViT代码和论文下载

leetcode-592:分数加减运算

Relationship between subnet number, host number and subnet mask

JVM之垃圾回收机制(GC)

循环神经网络RNN

CNN convolutional neural network

CVPR2022Oral专题系列(一):低光增强
随机推荐
Let the computer run only one program setting
Recurrent neural network RNN
【解决方案】ERROR: lib/bridge_generated.dart:837:9: Error: The parameter ‘ptr‘ of the method ‘FlutterRustB
Use of PDO
Teacher wangshuyao's notes on operations research 01 guidance and introduction
'function VTable for error: undefined reference to... 'cause and solution of the problem
王树尧老师运筹学课程笔记 10 线性规划与单纯形法(关于检测数与退化的讨论)
Share some tips for better code, smooth coding and improve efficiency
finally 和 return 的执行顺序
IDEA中实现Mapper接口到映射文件xml的跳转
【冷冻电镜|论文阅读】emClarity:用于高分辨率冷冻电子断层扫描和子断层平均的软件
模拟卷Leetcode【普通】061. 旋转链表
N2 interface of 5g control plane protocol
Teacher Cui Xueting's course notes on optimization theory and methods 00 are written in the front
【干货备忘】50种Matplotlib科研论文绘图合集,含代码实现
Actual combat! Talk about how to solve the deep paging problem of MySQL
Embedding理解+代码
Apisik health check test
Why does 5g N2 interface control plane use SCTP protocol?
Enterprise manager cannot connect to the database instance in Oracle10g solution