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

边栏推荐
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- Unpool(nn.MaxUnpool2d)
- [Yu Yue education] National Open University spring 2019 0505-22t basic nursing reference questions
- Last week's hot review (2.7-2.13)
- Exploration of short text analysis in the field of medical and health (II)
- Practical case of SQL optimization: speed up your database
- STL container
- Interpretation of mask RCNN paper
- Win: add general users to the local admins group
- Exploration of short text analysis in the field of medical and health (I)
猜你喜欢

Application and Optimization Practice of redis in vivo push platform

R language uses logistic regression and afrima, ARIMA time series models to predict world population

A tab Sina navigation bar

Summary and practice of knowledge map construction technology

Introduce reflow & repaint, and how to optimize it?

Marubeni Baidu applet detailed configuration tutorial, approved.

Interesting practice of robot programming 15- autoavoidobstacles

MySQL backup and recovery + experiment
![[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

Learn game model 3D characters, come out to find a job?
随机推荐
[download white paper] does your customer relationship management (CRM) really "manage" customers?
I use these six code comparison tools
Limited query of common SQL operations
Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym
Talk about the things that must be paid attention to when interviewing programmers
Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
How to build a technical team that will bring down the company?
Security level
Introduce reflow & repaint, and how to optimize it?
[technology development-26]: data security of new information and communication networks
Android advanced interview question record in 2022
Summary of regularization methods
Application and Optimization Practice of redis in vivo push platform
WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
Can you really learn 3DMAX modeling by self-study?
官宣!第三届云原生编程挑战赛正式启动!
Grub 2.12 will be released this year to continue to improve boot security
Visual explanation of Newton iteration method