Skip to content

Commit

Permalink
404.左叶子之和增加Go递归精简版
Browse files Browse the repository at this point in the history
  • Loading branch information
markwang1992 committed Jul 10, 2024
1 parent b7eb403 commit 6fd1df5
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions problems/0404.左叶子之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6fd1df5

Please sign in to comment.