Skip to content

Commit 23cabc4

Browse files
author
liwentian
committed
fd
1 parent 2f125e9 commit 23cabc4

File tree

5 files changed

+75
-42
lines changed

5 files changed

+75
-42
lines changed

ebook/tree/Tree.tex

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ \subsubsection{Solution I}
13231323
}
13241324

13251325
/**
1326-
* 一定要算上root
1326+
* 算上root
13271327
*/
13281328
private void helper(TreeNode root, int sum, int[] count) {
13291329
if (root == null) {
@@ -1343,11 +1343,11 @@ \subsubsection{Solution I}
13431343

13441344
\subsubsection{Solution II}
13451345
\begin{Code}
1346-
/* 如果要给路径打出来
1347-
public int pathSum(TreeNode root, int sum) {
1346+
// 如果要给路径打出来
1347+
public List<String> pathSum(TreeNode root, int sum) {
13481348
List<String> result = new LinkedList<>();
13491349
pathSum(root, sum, result, "");
1350-
return result.size();
1350+
return result;
13511351
}
13521352

13531353
private void pathSum(TreeNode root, int sum, List<String> list, String path) {
@@ -1365,12 +1365,15 @@ \subsubsection{Solution II}
13651365
if (root == null) {
13661366
return;
13671367
}
1368+
1369+
String prefix = path.isEmpty() ? "" : path + "->";
1370+
13681371
if (root.val == sum) {
1369-
list.add(path + "->" + root.val);
1372+
list.add(prefix + root.val);
13701373
}
1371-
pathSumWithRoot(root.left, sum - root.val, list, path + "->" + root.val);
1372-
pathSumWithRoot(root.right, sum - root.val, list, path + "->" + root.val);
1373-
}*/
1374+
pathSumWithRoot(root.left, sum - root.val, list, prefix + root.val);
1375+
pathSumWithRoot(root.right, sum - root.val, list, prefix + root.val);
1376+
}
13741377
\end{Code}
13751378

13761379
\newpage

library/src/main/java/com/leetcode/library/TreeNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public TreeNode(int x) {
1414
val = x;
1515
}
1616

17-
TreeNode(int x, TreeNode left, TreeNode right) {
17+
public TreeNode(int x, TreeNode left, TreeNode right) {
1818
this.val = x;
1919
this.left = left;
2020
this.right = right;

solution/src/main/java/com/inuker/solution/BinaryTreePreorderTraversal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
public class BinaryTreePreorderTraversal {
1515

16-
public List<Integer> preorderTraversal2(TreeNode root) {
16+
public List<Integer> preorderTraversal(TreeNode root) {
1717
List<Integer> result = new ArrayList<>();
1818
Deque<TreeNode> stack = new ArrayDeque<>();
1919
while(!stack.isEmpty() || root != null) {

solution/src/main/java/com/inuker/solution/PathSumIII.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private void helperSum(TreeNode root, int sum, int[] count) {
4343
}
4444

4545
/**
46-
* 一定要算上root
46+
* 算上root
4747
*/
4848
private void helper(TreeNode root, int sum, int[] count) {
4949
if (root == null) {
@@ -59,10 +59,10 @@ private void helper(TreeNode root, int sum, int[] count) {
5959
}
6060

6161
/* 如果要给路径打出来
62-
public int pathSum(TreeNode root, int sum) {
62+
public List<String> pathSum(TreeNode root, int sum) {
6363
List<String> result = new LinkedList<>();
6464
pathSum(root, sum, result, "");
65-
return result.size();
65+
return result;
6666
}
6767
6868
private void pathSum(TreeNode root, int sum, List<String> list, String path) {
@@ -80,10 +80,13 @@ private void pathSumWithRoot(TreeNode root, int sum, List<String> list, String p
8080
if (root == null) {
8181
return;
8282
}
83+
84+
String prefix = path.isEmpty() ? "" : path + "->";
85+
8386
if (root.val == sum) {
84-
list.add(path + "->" + root.val);
87+
list.add(prefix + root.val);
8588
}
86-
pathSumWithRoot(root.left, sum - root.val, list, path + "->" + root.val);
87-
pathSumWithRoot(root.right, sum - root.val, list, path + "->" + root.val);
89+
pathSumWithRoot(root.left, sum - root.val, list, prefix + root.val);
90+
pathSumWithRoot(root.right, sum - root.val, list, prefix + root.val);
8891
}*/
8992
}
Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,72 @@
11
package com.example;
22

33

4+
import com.inuker.solution.BinaryTreePreorderTraversal;
5+
import com.inuker.solution.PathSumIII;
6+
import com.leetcode.library.TreeNode;
7+
48
import java.sql.Timestamp;
59
import java.util.Calendar;
10+
import java.util.LinkedList;
11+
import java.util.List;
612

713
public class Main {
814

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);
1738
}
18-
return sb.toString().trim();
1939
}
2040

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+
}
2346

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+
}
2551

26-
System.out.println(s);
52+
pathSumWithRoot(root, sum, list, path);
2753

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+
}
3157

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 + "->";
3464

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);
4370
}
4471
}
4572

0 commit comments

Comments
 (0)