Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6fd1df5

Browse files
404.左叶子之和增加Go递归精简版
1 parent b7eb403 commit 6fd1df5

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

‎problems/0404.左叶子之和.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,21 @@ func sumOfLeftLeaves(root *TreeNode) int {
337337
}
338338
```
339339

340+
**递归精简版**
341+
342+
```go
343+
func sumOfLeftLeaves(root *TreeNode) int {
344+
if root == nil {
345+
return 0
346+
}
347+
leftValue := 0
348+
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
349+
leftValue = root.Left.Val
350+
}
351+
return leftValue + sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right)
352+
}
353+
```
354+
340355
**迭代法(前序遍历)**
341356

342357
```go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /