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
|
public static List<List<Integer>> pathSum(TreeNode root, int aim) {
List<List<Integer>> ans = new ArrayList<>();
if (root != null) {
List<Integer> path = new ArrayList<>();
f(root, aim, 0, path, ans);
}
return ans;
}
public static void f(TreeNode cur, int aim, int sum,
List<Integer> path, List<List<Integer>> ans) {
if (cur.left == null && cur.right == null) {
// 叶节点
if (cur.val + sum == aim) {
path.add(cur.val);
copy(path, ans);
path.remove(path.size() - 1);
}
} else {
// 非叶节点
path.add(cur.val);
if (cur.left != null) {
f(cur.left, aim, sum + cur.val, path, ans);
}
if (cur.right != null) {
f(cur.right, aim, sum + cur.val, path, ans);
}
path.remove(path.size() - 1);
}
}
public static void copy(List<Integer> path, List<List<Integer>> ans) {
List<Integer> copy = new ArrayList<>();
for (Integer num : path) {
copy.add(num);
}
ans.add(copy);
}
|