|
5 | 5 | import java.util.ArrayList;
|
6 | 6 | import java.util.List;
|
7 | 7 |
|
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 |
| - */ |
28 | 8 | 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 | + } |
61 | 36 | }
|
62 | 37 |
|
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 | + } |
71 | 52 | }
|
72 |
| - } |
73 | 53 |
|
74 | 54 | }
|
0 commit comments