8000 refactor 129 · FDSMlhn/Leetcode@9c5e5a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c5e5a9

Browse files
refactor 129
1 parent 5c2ba6f commit 9c5e5a9

File tree

1 file changed

+40
-35
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+40
-35
lines changed

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

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8-
/**Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
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.
912
1013
An example is the root-to-leaf path 1->2->3 which represents the number 123.
1114
@@ -23,47 +26,49 @@
2326
2427
*/
2528
public class _129 {
29+
public static class Solution1 {
2630
public int sumNumbers(TreeNode root) {
27-
if (root == null) {
28-
return 0;
29-
}
30-
List<Integer> allNumbers = new ArrayList();
31-
dfs(root, new StringBuilder(), allNumbers);
32-
int sum = 0;
33-
for (int i : allNumbers) {
34-
sum += i;
35-
}
36-
return sum;
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;
3741
}
3842

3943
private void dfs(TreeNode root, StringBuilder sb, List<Integer> allNumbers) {
40-
sb.append(root.val);
41-
if (root.left != null) {
42-
dfs(root.left, sb, allNumbers);
43-
}
44-
if (root.right != null) {
45-
dfs(root.right, sb, allNumbers);
46-
}
47-
if (root.left == null && root.right == null) {
48-
allNumbers.add(Integer.parseInt(sb.toString()));
49-
}
50-
sb.deleteCharAt(sb.length() - 1);
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);
5155
}
56+
}
5257

53-
class MoreConciseVersion {
54-
public int sumNumbers(TreeNode root) {
55-
return dfs(root, 0);
56-
}
58+
public static class Solution2 {
59+
public int sumNumbers(TreeNode root) {
60+
return dfs(root, 0);
61+
}
5762

58-
private int dfs(TreeNode root, int sum) {
59-
if (root == null) {
60-
return 0;
61-
}
62-
if (root.left == null && root.right == null) {
63-
return sum * 10 + root.val;
64-
}
65-
return dfs(root.left, sum * 10 + root.val) + dfs(root.right, sum * 10 + root.val);
66-
}
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);
6771
}
72+
}
6873

6974
}

0 commit comments

Comments
 (0)
< 293F /turbo-frame>
0