当前位置:网站首页>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 !
边栏推荐
- DBAsql面试题
- 【论文阅读】TomoAlign: A novel approach to correcting sample motion and 3D CTF in CryoET
- Difference between CNAME record and a record
- MySql基础知识(高频面试题)
- 5g service interface and reference point
- Unity免费元素特效推荐
- C语言数据类型
- 模拟卷Leetcode【普通】081. 搜索旋转排序数组 II
- leetcode-592:分数加减运算
- 【冷冻电镜|论文阅读】A feature-guided, focused 3D signal permutation method for subtomogram averaging
猜你喜欢

Software definition boundary SDP

10 frequently asked JVM questions in interviews

Apisik health check test

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

DM数据守护集群搭建

SDN topology discovery principle

How to write controller layer code gracefully?

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

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

吴恩达老师机器学习课程笔记 02 单变量线性回归
随机推荐
STP spanning tree principle and example of election rules
Teacher wangshuyao's notes on operations research 04 fundamentals of linear algebra
【冷冻电镜入门】加州理工公开课课程笔记 Part 3: Image Formation
Database multi table query joint query add delete modify query
【笔记】The art of research - (讲好故事和论点)
Teacher wangshuyao's notes on operations research 06 linear programming and simplex method (geometric significance)
10 frequently asked JVM questions in interviews
Teacher Wu Enda machine learning course notes 05 octave tutorial
Embedding understanding + code
数据库持久化+JDBC数据库连接
Biased lock, lightweight lock test tool class level related commands
How to write controller layer code gracefully?
王树尧老师运筹学课程笔记 07 线性规划与单纯形法(标准型、基、基解、基可行解、可行基)
王树尧老师运筹学课程笔记 00 写在前面
Share some tips for better code, smooth coding and improve efficiency
基于Matlab解决线性规划问题
【技能积累】写邮件时的常用表达
Difference between CNAME record and a record
剑指 Offer II 115:重建序列
【技能积累】presentation实用技巧积累,常用句式