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

边栏推荐
- He was laid off.. 39 year old Ali P9, saved 150million
- Collection of gmat750 wrong questions
- Introduce reflow & repaint, and how to optimize it?
- Tucson will lose more than $400million in the next year
- Process scheduling and termination
- Blue bridge - maximum common divisor and minimum common multiple
- Application and Optimization Practice of redis in vivo push platform
- Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
- One plus six brushes into Kali nethunter
- Binary tree traversal - middle order traversal (golang)
猜你喜欢

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

How to make a cool ink screen electronic clock?

Practice of tdengine in TCL air conditioning energy management platform

Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down

Codeforces Global Round 19 ABC

力扣剑指offer——二叉树篇

Visual studio 2019 set transparent background (fool teaching)

Yyds dry inventory swagger positioning problem ⽅ formula

Icu4c 70 source code download and compilation (win10, vs2022)

Open source SPL optimized report application coping endlessly
随机推荐
I use these six code comparison tools
Grub 2.12 will be released this year to continue to improve boot security
Li Kou Jianzhi offer -- binary tree chapter
Start the remedial work. Print the contents of the array using the pointer
The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
Codeforces Global Round 19 ABC
Variables in postman
100 basic multiple choice questions of C language (with answers) 04
Rabbit MQ message sending of vertx
172. Zero after factorial
[机缘参悟-38]:鬼谷子-第五飞箝篇 - 警示之一:有一种杀称为“捧杀”
Pytorch register_ Hook (operate on gradient grad)
Pytorch fine tuning (Fortune): hollowed out design or cheating
丸子百度小程序详细配置教程,审核通过。
MATLB | multi micro grid and distributed energy trading
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
Practice of tdengine in TCL air conditioning energy management platform
Win: enable and disable USB drives using group policy
Visual explanation of Newton iteration method
Restful Fast Request 2022.2.1发布,支持cURL导入