Skip to content

Commit

Permalink
添加0222.完全二叉树的节点个数Go版本.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Galaxies2580 committed Sep 26, 2022
1 parent 8309641 commit 37d518f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions problems/0222.完全二叉树的节点个数.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,33 @@ func countNodes(root *TreeNode) int {
}
```

迭代法

```go
func countNodes(root *TreeNode) int {
if root == nil {
return 0
}
q := list.New()
q.PushBack(root)
res := 0
for q.Len() > 0 {
n := q.Len()
for i := 0; i < n; i++ {
node := q.Remove(q.Front()).(*TreeNode)
if node.Left != nil {
q.PushBack(node.Left)
}
if node.Right != nil {
q.PushBack(node.Right)
}
res++
}
}
return res
}
```



## JavaScript:
Expand Down

0 comments on commit 37d518f

Please sign in to comment.