添加 0222.完全二叉树的节点个数 go版本

添加 0222.完全二叉树的节点个数 go版本
This commit is contained in:
X-shuffle
2021-06-06 22:13:15 +08:00
committed by GitHub
parent 0a2a5b180a
commit 39f852e656

View File

@ -308,6 +308,35 @@ class Solution:
Go
递归版本
```go
/**
* 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
}
res := 1
if root.Right != nil {
res += countNodes(root.Right)
}
if root.Left != nil {
res += countNodes(root.Left)
}
return res
}
```
JavaScript:
递归版本