Skip to content

Commit 7ca6279

Browse files
authored
Merge pull request halfrost#141 from novahe/fix/129
update 129
2 parents 95eddf7 + 44eddae commit 7ca6279

File tree

3 files changed

+26
-44
lines changed

3 files changed

+26
-44
lines changed
Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package leetcode
22

3-
import (
4-
"strconv"
5-
)
6-
73
import (
84
"github.com/halfrost/LeetCode-Go/structures"
95
)
@@ -21,29 +17,20 @@ type TreeNode = structures.TreeNode
2117
*/
2218

2319
func sumNumbers(root *TreeNode) int {
24-
res, nums := 0, binaryTreeNums(root)
25-
for _, n := range nums {
26-
num, _ := strconv.Atoi(n)
27-
res += num
28-
}
20+
res := 0
21+
dfs(root, 0, &res)
2922
return res
3023
}
3124

32-
func binaryTreeNums(root *TreeNode) []string {
25+
func dfs(root *TreeNode, sum int, res *int) {
3326
if root == nil {
34-
return []string{}
27+
return
3528
}
36-
res := []string{}
29+
sum = sum*10 + root.Val
3730
if root.Left == nil && root.Right == nil {
38-
return []string{strconv.Itoa(root.Val)}
31+
*res += sum
32+
return
3933
}
40-
tmpLeft := binaryTreeNums(root.Left)
41-
for i := 0; i < len(tmpLeft); i++ {
42-
res = append(res, strconv.Itoa(root.Val)+tmpLeft[i])
43-
}
44-
tmpRight := binaryTreeNums(root.Right)
45-
for i := 0; i < len(tmpRight); i++ {
46-
res = append(res, strconv.Itoa(root.Val)+tmpRight[i])
47-
}
48-
return res
34+
dfs(root.Left, sum, res)
35+
dfs(root.Right, sum, res)
4936
}

leetcode/0129.Sum-Root-to-Leaf-Numbers/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ Find the total sum of all root-to-leaf numbers.
4545

4646
## 解题思路
4747

48-
- 这一题是第 257 题的变形题,第 257 题要求输出每条从根节点到叶子节点的路径,这一题变成了把每一个从根节点到叶子节点的数字都串联起来,再累加每条路径,求出最后的总和。实际做题思路基本没变
48+
- 运用前序遍历的思想,当从根节点出发一直加到叶子节点,每个叶子节点汇总一次。

website/content/ChapterFour/0100~0199/0129.Sum-Root-to-Leaf-Numbers.md

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ Find the total sum of all root-to-leaf numbers.
5555
package leetcode
5656

5757
import (
58-
"strconv"
58+
"github.com/halfrost/LeetCode-Go/structures"
5959
)
6060

61+
// TreeNode define
62+
type TreeNode = structures.TreeNode
63+
6164
/**
6265
* Definition for a binary tree node.
6366
* type TreeNode struct {
@@ -66,32 +69,24 @@ import (
6669
* Right *TreeNode
6770
* }
6871
*/
72+
6973
func sumNumbers(root *TreeNode) int {
70-
res, nums := 0, binaryTreeNums(root)
71-
for _, n := range nums {
72-
num, _ := strconv.Atoi(n)
73-
res += num
74-
}
74+
res := 0
75+
dfs(root,0,&res)
7576
return res
7677
}
7778

78-
func binaryTreeNums(root *TreeNode) []string {
79-
if root == nil {
80-
return []string{}
79+
func dfs(root *TreeNode,sum int,res *int) {
80+
if root == nil{
81+
return
8182
}
82-
res := []string{}
83-
if root.Left == nil && root.Right == nil {
84-
return []string{strconv.Itoa(root.Val)}
83+
sum = sum*10 + root.Val
84+
if root.Left == nil && root.Right == nil{
85+
*res += sum
86+
return
8587
}
86-
tmpLeft := binaryTreeNums(root.Left)
87-
for i := 0; i < len(tmpLeft); i++ {
88-
res = append(res, strconv.Itoa(root.Val)+tmpLeft[i])
89-
}
90-
tmpRight := binaryTreeNums(root.Right)
91-
for i := 0; i < len(tmpRight); i++ {
92-
res = append(res, strconv.Itoa(root.Val)+tmpRight[i])
93-
}
94-
return res
88+
dfs(root.Left,sum,res)
89+
dfs(root.Right,sum,res)
9590
}
9691

9792
```

0 commit comments

Comments
 (0)