mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
更新0129.求根到叶子结点数字之和中,go语言代码无法通过leetcode的语法错误
修改了原本的go代码的错误(原本的go代码无法通过leetcode的题目执行,有语法错误)
This commit is contained in:
@ -250,22 +250,22 @@ Go:
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
func sumNumbers(root *TreeNode) int {
|
func sumNumbers(root *TreeNode) int {
|
||||||
sum = 0
|
sum := 0
|
||||||
travel(root, root.Val)
|
dfs(root, root.Val, &sum)
|
||||||
return sum
|
return sum
|
||||||
}
|
}
|
||||||
|
|
||||||
func travel(root *TreeNode, tmpSum int) {
|
func dfs(root *TreeNode, tmpSum int, sum *int) {
|
||||||
if root.Left == nil && root.Right == nil {
|
if root.Left == nil && root.Right == nil {
|
||||||
sum += tmpSum
|
*sum += tmpSum
|
||||||
} else {
|
} else {
|
||||||
if root.Left != nil {
|
if root.Left != nil {
|
||||||
travel(root.Left, tmpSum*10+root.Left.Val)
|
dfs(root.Left, tmpSum*10 + root.Left.Val, sum)
|
||||||
}
|
}
|
||||||
if root.Right != nil {
|
if root.Right != nil {
|
||||||
travel(root.Right, tmpSum*10+root.Right.Val)
|
dfs(root.Right, tmpSum*10 + root.Right.Val, sum)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user