当前位置:网站首页>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 !
边栏推荐
- 【干货备忘】50种Matplotlib科研论文绘图合集,含代码实现
- Hongke share | let you have a comprehensive understanding of "can bus error" (III) -- can node status and error counter
- ECCV 2022丨轻量级模型架ParC-Net 力压苹果MobileViT代码和论文下载
- 王树尧老师运筹学课程笔记 06 线性规划与单纯形法(几何意义)
- Salesforce中过滤器Filter使用的相对日期
- 模拟卷Leetcode【普通】081. 搜索旋转排序数组 II
- 数据单位:位、字节、字、字长
- DBAsql面试题
- Use of callable
- Security in quantum machine learning
猜你喜欢
新同事写了几段小代码,把系统给搞崩了,被老板爆怼一顿!
【经验】通过跳板机远程连接内网服务器的相关配置
王树尧老师运筹学课程笔记 10 线性规划与单纯形法(关于检测数与退化的讨论)
JMM memory model concept
C语言数据类型
Hongke shares | testing and verifying complex FPGA design (2) -- how to perform global oriented simulation in IP core
Share some tips for better code, smooth coding and improve efficiency
DM数据守护集群搭建
Understanding of access, hybrid and trunk modes
SDN topology discovery principle
随机推荐
Teacher Wu Enda's machine learning course notes 00 are written in the front
线程同步—— 生产者与消费者、龟兔赛跑、双线程打印
新同事写了几段小代码,把系统给搞崩了,被老板爆怼一顿!
【冷冻电镜】RELION4.0之subtomogram对位功能源码分析(自用)
Embedding理解+代码
Hongke share | let you have a comprehensive understanding of "can bus error" (III) -- can node status and error counter
The difference between pairs and ipairs
吴恩达老师机器学习课程笔记 01 引言
【冷冻电镜】RELION4.0 pipeline命令总结(自用)
SS command details
【论文阅读】TomoAlign: A novel approach to correcting sample motion and 3D CTF in CryoET
Basic knowledge of MySQL (high frequency interview questions)
分享一些你代码更好的小建议,流畅编码提搞效率
二次元卡通渲染——进阶技巧
C language memory stack and heap usage
数据库系统概述
2022年SQL经典面试题总结(带解析)
矩阵分解与梯度下降
leetcode-592:分数加减运算
Phantom reference virtual reference code demonstration