mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
添加(0129.求根节点到叶节点数字之和.md):增加go版本
This commit is contained in:
@ -3,6 +3,9 @@
|
||||
<img src="https://code-thinking-1253855093.file.myqcloud.com/pics/20210924105952.png" width="1000"/>
|
||||
</a>
|
||||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
|
||||
|
||||
# 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) {
|
||||
|
Reference in New Issue
Block a user