Skip to content

Commit 8098196

Browse files
refactor 129
1 parent 6abfc30 commit 8098196

File tree

1 file changed

+41
-61
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+41
-61
lines changed

src/main/java/com/fishercoder/solutions/_129.java

Lines changed: 41 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,50 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8-
/**
9-
* 129. Sum Root to Leaf Numbers
10-
*
11-
* Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
12-
13-
An example is the root-to-leaf path 1->2->3 which represents the number 123.
14-
15-
Find the total sum of all root-to-leaf numbers.
16-
17-
For example,
18-
19-
1
20-
/ \
21-
2 3
22-
The root-to-leaf path 1->2 represents the number 12.
23-
The root-to-leaf path 1->3 represents the number 13.
24-
25-
Return the sum = 12 + 13 = 25.
26-
27-
*/
288
public class _129 {
29-
public static class Solution1 {
30-
public int sumNumbers(TreeNode root) {
31-
if (root == null) {
32-
return 0;
33-
}
34-
List<Integer> allNumbers = new ArrayList();
35-
dfs(root, new StringBuilder(), allNumbers);
36-
int sum = 0;
37-
for (int i : allNumbers) {
38-
sum += i;
39-
}
40-
return sum;
41-
}
42-
43-
private void dfs(TreeNode root, StringBuilder sb, List<Integer> allNumbers) {
44-
sb.append(root.val);
45-
if (root.left != null) {
46-
dfs(root.left, sb, allNumbers);
47-
}
48-
if (root.right != null) {
49-
dfs(root.right, sb, allNumbers);
50-
}
51-
if (root.left == null && root.right == null) {
52-
allNumbers.add(Integer.parseInt(sb.toString()));
53-
}
54-
sb.deleteCharAt(sb.length() - 1);
55-
}
56-
}
57-
58-
public static class Solution2 {
59-
public int sumNumbers(TreeNode root) {
60-
return dfs(root, 0);
9+
public static class Solution1 {
10+
public int sumNumbers(TreeNode root) {
11+
if (root == null) {
12+
return 0;
13+
}
14+
List<Integer> allNumbers = new ArrayList();
15+
dfs(root, new StringBuilder(), allNumbers);
16+
int sum = 0;
17+
for (int i : allNumbers) {
18+
sum += i;
19+
}
20+
return sum;
21+
}
22+
23+
private void dfs(TreeNode root, StringBuilder sb, List<Integer> allNumbers) {
24+
sb.append(root.val);
25+
if (root.left != null) {
26+
dfs(root.left, sb, allNumbers);
27+
}
28+
if (root.right != null) {
29+
dfs(root.right, sb, allNumbers);
30+
}
31+
if (root.left == null && root.right == null) {
32+
allNumbers.add(Integer.parseInt(sb.toString()));
33+
}
34+
sb.deleteCharAt(sb.length() - 1);
35+
}
6136
}
6237

63-
private int dfs(TreeNode root, int sum) {
64-
if (root == null) {
65-
return 0;
66-
}
67-
if (root.left == null && root.right == null) {
68-
return sum * 10 + root.val;
69-
}
70-
return dfs(root.left, sum * 10 + root.val) + dfs(root.right, sum * 10 + root.val);
38+
public static class Solution2 {
39+
public int sumNumbers(TreeNode root) {
40+
return dfs(root, 0);
41+
}
42+
43+
private int dfs(TreeNode root, int sum) {
44+
if (root == null) {
45+
return 0;
46+
}
47+
if (root.left == null && root.right == null) {
48+
return sum * 10 + root.val;
49+
}
50+
return dfs(root.left, sum * 10 + root.val) + dfs(root.right, sum * 10 + root.val);
51+
}
7152
}
72-
}
7353

7454
}

0 commit comments

Comments
 (0)