-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPathSumII.java
38 lines (35 loc) · 1.05 KB
/
PathSumII.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
/**
* 113. Path Sum II
* @author LBW
*/
public class PathSumII {
private List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<Integer> l = new ArrayList<>();
findPathSum(root, l, 0, sum);
return result;
}
private void findPathSum(TreeNode root, List<Integer> l, int cur, int sum) {
if (root == null)
return;
l.add(root.val);
int t = cur + root.val;
if (root.left == null && root.right == null) {
if (t == sum) {
List<Integer> newl = new ArrayList<>(l);
result.add(newl);
}
}
findPathSum(root.left, l, t, sum);
findPathSum(root.right, l, t, sum);
l.remove(l.size() - 1);
cur -= root.val; //这句没啥用吧
}
public static void main(String[] args) {
PathSumII pathSumII = new PathSumII();
pathSumII.pathSum(null, 0);
}
}