当前位置:网站首页>【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
}
边栏推荐
- Some query constructors in laravel (2)
- Application and Optimization Practice of redis in vivo push platform
- Valentine's Day flirting with girls to force a small way, one can learn
- Action News
- Huawei machine test question: longest continuous subsequence
- Start the remedial work. Print the contents of the array using the pointer
- Win: use PowerShell to check the strength of wireless signal
- Android advanced interview question record in 2022
- 如何搭建一支搞垮公司的技术团队?
- WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
猜你喜欢
R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口
Action News
Introduce reflow & repaint, and how to optimize it?
Win: use shadow mode to view the Desktop Session of a remote user
The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
Prometheus monitors the correct posture of redis cluster
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
How to make a cool ink screen electronic clock?
Five ways to query MySQL field comments!
丸子百度小程序详细配置教程,审核通过。
随机推荐
如何做一个炫酷的墨水屏电子钟?
Grub 2.12 will be released this year to continue to improve boot security
Open source SPL optimized report application coping endlessly
A tab Sina navigation bar
pytorch fine-tuning (funtune) : 镂空设计or 偷梁换柱
How to make a cool ink screen electronic clock?
Win:使用 Shadow Mode 查看远程用户的桌面会话
Richview trvunits image display units
R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口
Win:将一般用户添加到 Local Admins 组中
[download white paper] does your customer relationship management (CRM) really "manage" customers?
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Three properties that a good homomorphic encryption should satisfy
[技术发展-26]:新型信息与通信网络的数据安全
A label colorful navigation bar
[illumination du destin - 38]: Ghost Valley - chapitre 5 Flying clamp - one of the Warnings: There is a kind of killing called "hold Kill"
One click generation and conversion of markdown directory to word format
The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
R language uses logistic regression and afrima, ARIMA time series models to predict world population
Action News