当前位置:网站首页>【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 MySQL team development specifications used by various factories are too detailed. It is recommended to collect them!
- Pytorch common code snippet collection
- openresty ngx_lua执行阶段
- Variables in postman
- I use these six code comparison tools
- Application and development trend of image recognition technology
- Do you know the eight signs of a team becoming agile?
- Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
- Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
- Win:使用 PowerShell 检查无线信号的强弱
猜你喜欢
Bert fine tuning skills experiment
openresty ngx_lua執行階段
How to build a technical team that will bring down the company?
Exploration of short text analysis in the field of medical and health (II)
Yyds dry inventory swagger positioning problem ⽅ formula
Three properties that a good homomorphic encryption should satisfy
Binary tree traversal - middle order traversal (golang)
如何搭建一支搞垮公司的技術團隊?
Introduce reflow & repaint, and how to optimize it?
Open source SPL optimized report application coping endlessly
随机推荐
LeetCode 314. Binary tree vertical order traversal - Binary Tree Series Question 6
R language uses logistic regression and afrima, ARIMA time series models to predict world population
Do you know the eight signs of a team becoming agile?
The most powerful new household god card of Bank of communications. Apply to earn 2100 yuan. Hurry up if you haven't applied!
The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
Matrixone 0.2.0 is released, and the fastest SQL computing engine is coming
MySQL backup and recovery + experiment
如何搭建一支搞垮公司的技術團隊?
Visual explanation of Newton iteration method
CAM Pytorch
Pytorch common code snippet collection
runc hang 导致 Kubernetes 节点 NotReady
179. Maximum number - sort
. Net starts again happy 20th birthday
Codeforces Round #770 (Div. 2) ABC
Valentine's Day flirting with girls to force a small way, one can learn
Learn game model 3D characters, come out to find a job?
Three properties that a good homomorphic encryption should satisfy
172. Zero after factorial
Security level