当前位置:网站首页>【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
}

边栏推荐
- Redis distributed lock, lock code logic
- R language uses logistic regression and afrima, ARIMA time series models to predict world population
- Flutter 2.10 update details
- Uniapp navigateto jump failure
- [機緣參悟-38]:鬼穀子-第五飛箝篇 - 警示之一:有一種殺稱為“捧殺”
- Win:使用 Shadow Mode 查看远程用户的桌面会话
- LeetCode 314. Binary tree vertical order traversal - Binary Tree Series Question 6
- 85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
- [download white paper] does your customer relationship management (CRM) really "manage" customers?
- Visual studio 2019 set transparent background (fool teaching)
猜你喜欢
![[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation](/img/9e/4c8557bb4b75b1e74598dedb24af86.jpg)
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation

Application and Optimization Practice of redis in vivo push platform

openresty ngx_lua執行階段

Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)

Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool

Yolov5 model training and detection

Interesting practice of robot programming 15- autoavoidobstacles

Prometheus monitors the correct posture of redis cluster

Go RPC call

Practice of tdengine in TCL air conditioning energy management platform
随机推荐
Leetcode takes out the least number of magic beans
220213c language learning diary
Application and development trend of image recognition technology
Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
100 basic multiple choice questions of C language (with answers) 04
Application and Optimization Practice of redis in vivo push platform
Talk about the things that must be paid attention to when interviewing programmers
Interesting practice of robot programming 15- autoavoidobstacles
Five ways to query MySQL field comments!
Interpretation of mask RCNN paper
Win:将一般用户添加到 Local Admins 组中
STL container
LeetCode 314. Binary tree vertical order traversal - Binary Tree Series Question 6
Include rake tasks in Gems - including rake tasks in gems
Win: add general users to the local admins group
Talk about the things that must be paid attention to when interviewing programmers
Li Kou Jianzhi offer -- binary tree chapter
Runc hang causes the kubernetes node notready
When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
RichView TRVStyle MainRVStyle