From 091204c926a044f0ec86200cbdd3cca6deeaf97e Mon Sep 17 00:00:00 2001
From: HanMengnan <1448189829@qq.com>
Date: Tue, 7 Jun 2022 10:46:43 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880129.=E6=B1=82?=
=?UTF-8?q?=E6=A0=B9=E8=8A=82=E7=82=B9=E5=88=B0=E5=8F=B6=E8=8A=82=E7=82=B9?=
=?UTF-8?q?=E6=95=B0=E5=AD=97=E4=B9=8B=E5=92=8C.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0go=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0129.求根到叶子节点数字之和.md | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/problems/0129.求根到叶子节点数字之和.md b/problems/0129.求根到叶子节点数字之和.md
index b271ca7d..4191bb26 100644
--- a/problems/0129.求根到叶子节点数字之和.md
+++ b/problems/0129.求根到叶子节点数字之和.md
@@ -3,6 +3,9 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+ + + # 129. 求根节点到叶节点数字之和 [力扣题目链接](https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/) @@ -245,6 +248,29 @@ class Solution: ``` Go: +```go +func sumNumbers(root *TreeNode) int { + sum = 0 + travel(root, root.Val) + return sum +} + +func travel(root *TreeNode, tmpSum int) { + if root.Left == nil && root.Right == nil { + sum += tmpSum + } else { + if root.Left != nil { + travel(root.Left, tmpSum*10+root.Left.Val) + } + if root.Right != nil { + travel(root.Right, tmpSum*10+root.Right.Val) + } + } +} +``` + + + JavaScript: ```javascript var sumNumbers = function(root) {