From 6fd1df5bd7c6e665d6caf56368bf0289c2c27cdc Mon Sep 17 00:00:00 2001 From: markwang Date: Wed, 10 Jul 2024 10:19:39 +0800 Subject: [PATCH] =?UTF-8?q?404.=E5=B7=A6=E5=8F=B6=E5=AD=90=E4=B9=8B?= =?UTF-8?q?=E5=92=8C=E5=A2=9E=E5=8A=A0Go=E9=80=92=E5=BD=92=E7=B2=BE?= =?UTF-8?q?=E7=AE=80=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...17\266\345\255\220\344\271\213\345\222\214.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" index 1ba71dc94f..66aff68f2e 100644 --- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" +++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" @@ -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