当前位置:网站首页>【LeetCode】222. The number of nodes of a complete binary tree (2 mistakes)
【LeetCode】222. The number of nodes of a complete binary tree (2 mistakes)
2022-07-05 02:20:00 【Kaimar】
- Ideas
It's easy to think of using any traversal method to traverse all nodes . Another idea is to notice that the title says it is a complete binary tree , Then we can use its nature , That is, for a full binary tree , Its number of nodes is related to its height 2ⁿ-1, Refer to the explanation of the question Know the depth of traversing the left and right subtrees , If the depth is the same, it means that it is a full binary tree , Available formulas .
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */
func countNodes(root *TreeNode) int {
if root == nil {
return 0
}
queue := []*TreeNode{
root}
nodeCount := 0
for len(queue) > 0 {
size := len(queue)
for i := 0; i < size; i++ {
nodeCount++
// Value
node := queue[0]
// Out of the team
queue = queue[1:]
if node.Left != nil {
queue = append(queue, node.Left)
}
if node.Right != nil {
queue = append(queue, node.Right)
}
}
}
return nodeCount
}
边栏推荐
- Outlook:总是提示输入用户密码
- Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
- 如何做一个炫酷的墨水屏电子钟?
- Numpy library introductory tutorial: basic knowledge summary
- Li Kou Jianzhi offer -- binary tree chapter
- Lsblk command - check the disk of the system. I don't often use this command, but it's still very easy to use. Onion duck, like, collect, pay attention, wait for your arrival!
- Exploration of short text analysis in the field of medical and health (II)
- Traditional chips and AI chips
- 官宣!第三届云原生编程挑战赛正式启动!
- LeetCode 314. Binary tree vertical order traversal - Binary Tree Series Question 6
猜你喜欢
openresty ngx_lua執行階段
Prometheus monitors the correct posture of redis cluster
PowerShell:在代理服务器后面使用 PowerShell
力扣剑指offer——二叉树篇
One plus six brushes into Kali nethunter
PowerShell: use PowerShell behind the proxy server
Matrixone 0.2.0 is released, and the fastest SQL computing engine is coming
Summary and practice of knowledge map construction technology
Interesting practice of robot programming 15- autoavoidobstacles
Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide
随机推荐
Timescaledb 2.5.2 release, time series database based on PostgreSQL
Talk about the things that must be paid attention to when interviewing programmers
Codeforces Round #770 (Div. 2) ABC
Win: add general users to the local admins group
Matrixone 0.2.0 is released, and the fastest SQL computing engine is coming
Li Kou Jianzhi offer -- binary tree chapter
Yolov5 model training and detection
Process scheduling and termination
spoon插入更新oracle数据库,插了一部分提示报错Assertion botch: negative time
[機緣參悟-38]:鬼穀子-第五飛箝篇 - 警示之一:有一種殺稱為“捧殺”
Include rake tasks in Gems - including rake tasks in gems
Visual explanation of Newton iteration method
Advanced learning of MySQL -- Application -- Introduction
Icu4c 70 source code download and compilation (win10, vs2022)
Exploration of short text analysis in the field of medical and health (II)
Advanced conditional statements of common SQL operations
[机缘参悟-38]:鬼谷子-第五飞箝篇 - 警示之一:有一种杀称为“捧杀”
Win:使用 PowerShell 检查无线信号的强弱
A label colorful navigation bar
[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit