mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加 0222.完全二叉树的节点个数 go版本
添加 0222.完全二叉树的节点个数 go版本
This commit is contained in:
@ -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:
|
||||
|
||||
递归版本
|
||||
|
Reference in New Issue
Block a user