|
1 | 1 | package com.example; |
2 | 2 |
|
3 | 3 |
|
| 4 | +import com.inuker.solution.BinaryTreePreorderTraversal; |
| 5 | +import com.inuker.solution.PathSumIII; |
| 6 | +import com.leetcode.library.TreeNode; |
| 7 | + |
4 | 8 | import java.sql.Timestamp; |
5 | 9 | import java.util.Calendar; |
| 10 | +import java.util.LinkedList; |
| 11 | +import java.util.List; |
6 | 12 |
|
7 | 13 | public class Main { |
8 | 14 |
|
9 | | - private static String fd(String s) { |
10 | | - StringBuilder sb = new StringBuilder(); |
11 | | - for (char c : s.toCharArray()) { |
12 | | - if (Character.isDigit(c) || c == '-' || c == ':' || c == '.') { |
13 | | - sb.append(c); |
14 | | - } else { |
15 | | - sb.append(' '); |
16 | | - } |
| 15 | + public static void main(String[] args) { |
| 16 | + TreeNode nodef32 = new TreeNode(-3); |
| 17 | + TreeNode node32 = new TreeNode(3, nodef32, null); |
| 18 | + TreeNode nodef2 = new TreeNode(-2); |
| 19 | + TreeNode node1 = new TreeNode(1); |
| 20 | + TreeNode node11 = new TreeNode(11); |
| 21 | + |
| 22 | + TreeNode node3 = new TreeNode(3, node32, nodef2); |
| 23 | + TreeNode node2 = new TreeNode(2, null, node1); |
| 24 | + |
| 25 | + TreeNode node5 = new TreeNode(5, node3, node2); |
| 26 | + TreeNode nodef3 = new TreeNode(-3, null, node11); |
| 27 | + |
| 28 | + TreeNode root = new TreeNode(10, node5, nodef3); |
| 29 | + |
| 30 | +// List<Integer> list = new BinaryTreePreorderTraversal().preorderTraversal(root); |
| 31 | +// for (Integer n : list) { |
| 32 | +// System.out.print(n + " "); |
| 33 | +// } |
| 34 | + |
| 35 | + List<String> paths = pathSum(root, 8); |
| 36 | + for (String s : paths) { |
| 37 | + System.out.println(s); |
17 | 38 | } |
18 | | - return sb.toString().trim(); |
19 | 39 | } |
20 | 40 |
|
21 | | - public static void main(String[] args) { |
22 | | - String s = "2000-09-14T03:40:07.445Z"; |
| 41 | + public static List<String> pathSum(TreeNode root, int sum) { |
| 42 | + List<String> result = new LinkedList<>(); |
| 43 | + pathSum(root, sum, result, ""); |
| 44 | + return result; |
| 45 | + } |
23 | 46 |
|
24 | | - s = fd(s); |
| 47 | + private static void pathSum(TreeNode root, int sum, List<String> list, String path) { |
| 48 | + if (root == null) { |
| 49 | + return; |
| 50 | + } |
25 | 51 |
|
26 | | - System.out.println(s); |
| 52 | + pathSumWithRoot(root, sum, list, path); |
27 | 53 |
|
28 | | - Timestamp stamp = Timestamp.valueOf(s); |
29 | | - Calendar c = Calendar.getInstance(); |
30 | | - c.setTimeInMillis(stamp.getTime()); |
| 54 | + pathSum(root.left, sum, list, ""); |
| 55 | + pathSum(root.right, sum, list, ""); |
| 56 | + } |
31 | 57 |
|
32 | | - int max = c.getActualMaximum(Calendar.DAY_OF_YEAR); |
33 | | - System.out.println(max); |
| 58 | + private static void pathSumWithRoot(TreeNode root, int sum, List<String> list, String path) { |
| 59 | + if (root == null) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + String prefix = path.isEmpty() ? "" : path + "->"; |
34 | 64 |
|
35 | | - System.out.println(c.get(Calendar.YEAR)); |
36 | | - System.out.println(c.get(Calendar.MONTH) + 1); |
37 | | - System.out.println(c.get(Calendar.DATE)); |
38 | | - System.out.println(c.get(Calendar.DAY_OF_YEAR)); |
39 | | - System.out.println(c.get(Calendar.HOUR_OF_DAY)); |
40 | | - System.out.println(c.get(Calendar.MINUTE)); |
41 | | - System.out.println(c.get(Calendar.SECOND)); |
42 | | - System.out.println(c.get(Calendar.MILLISECOND)); |
| 65 | + if (root.val == sum) { |
| 66 | + list.add(prefix + root.val); |
| 67 | + } |
| 68 | + pathSumWithRoot(root.left, sum - root.val, list, prefix + root.val); |
| 69 | + pathSumWithRoot(root.right, sum - root.val, list, prefix + root.val); |
43 | 70 | } |
44 | 71 | } |
45 | 72 |
|
0 commit comments