|
5 | 5 | import java.util.ArrayList;
|
6 | 6 | import java.util.List;
|
7 | 7 |
|
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. |
9 | 12 |
|
10 | 13 | An example is the root-to-leaf path 1->2->3 which represents the number 123.
|
11 | 14 |
|
|
23 | 26 |
|
24 | 27 | */
|
25 | 28 | public class _129 {
|
| 29 | + public static class Solution1 { |
26 | 30 | 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; |
37 | 41 | }
|
38 | 42 |
|
39 | 43 | 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); |
51 | 55 | }
|
| 56 | + } |
52 | 57 |
|
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 | + } |
57 | 62 |
|
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); |
67 | 71 | }
|
| 72 | + } |
68 | 73 |
|
69 | 74 | }
|
0 commit comments