mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
@ -308,6 +308,35 @@ class Solution:
|
|||||||
|
|
||||||
Go:
|
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:
|
JavaScript:
|
||||||
|
|
||||||
递归版本
|
递归版本
|
||||||
|
Reference in New Issue
Block a user