当前位置:网站首页>【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
}
边栏推荐
- [download white paper] does your customer relationship management (CRM) really "manage" customers?
- Introduce reflow & repaint, and how to optimize it?
- Runc hang causes the kubernetes node notready
- [机缘参悟-38]:鬼谷子-第五飞箝篇 - 警示之一:有一种杀称为“捧杀”
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- [uc/os-iii] chapter 1.2.3.4 understanding RTOS
- The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
- pytorch fine-tuning (funtune) : 镂空设计or 偷梁换柱
- RichView TRVStyle MainRVStyle
- Interesting practice of robot programming 15- autoavoidobstacles
猜你喜欢
Learn game model 3D characters, come out to find a job?
openresty ngx_lua执行阶段
Variables in postman
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Exploration of short text analysis in the field of medical and health (I)
Application and Optimization Practice of redis in vivo push platform
Interesting practice of robot programming 15- autoavoidobstacles
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
Official announcement! The third cloud native programming challenge is officially launched!
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
随机推荐
[download white paper] does your customer relationship management (CRM) really "manage" customers?
220213c language learning diary
【附源码】基于知识图谱的智能推荐系统-Sylvie小兔
Valentine's Day flirting with girls to force a small way, one can learn
Li Kou Jianzhi offer -- binary tree chapter
Application and Optimization Practice of redis in vivo push platform
Unpool(nn.MaxUnpool2d)
Official announcement! The third cloud native programming challenge is officially launched!
Advanced learning of MySQL -- Application -- Introduction
Exploration of short text analysis in the field of medical and health (I)
Huawei machine test question: longest continuous subsequence
Pytorch fine tuning (Fortune): hollowed out design or cheating
He was laid off.. 39 year old Ali P9, saved 150million
LeetCode 314. Binary tree vertical order traversal - Binary Tree Series Question 6
When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
Summary of regularization methods
Video display and hiding of imitation tudou.com
179. Maximum number - sort
Practice of tdengine in TCL air conditioning energy management platform
Include rake tasks in Gems - including rake tasks in gems