mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Merge pull request #2606 from markwang1992/404-sumOfLeftLeaves
404.左叶子之和增加Go递归精简版
This commit is contained in:
@ -337,6 +337,21 @@ func sumOfLeftLeaves(root *TreeNode) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**递归精简版**
|
||||||
|
|
||||||
|
```go
|
||||||
|
func sumOfLeftLeaves(root *TreeNode) int {
|
||||||
|
if root == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
leftValue := 0
|
||||||
|
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
|
||||||
|
leftValue = root.Left.Val
|
||||||
|
}
|
||||||
|
return leftValue + sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
**迭代法(前序遍历)**
|
**迭代法(前序遍历)**
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user