Skip to content

Commit 87a6409

Browse files
committed
fd
1 parent 036597d commit 87a6409

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

leetcode/solution/src/PathSumII.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ private void helper(TreeNode node, int sum, List<List<Integer>> result, List<Int
1616
}
1717

1818
list.add(node.val);
19-
sum -= node.val;
2019

21-
if (node.left == null && node.right == null && sum == 0) {
20+
if (node.left == null && node.right == null && sum == node.val) {
2221
result.add(new ArrayList<>(list));
2322
} else {
24-
helper(node.left, sum, result, list);
25-
helper(node.right, sum, result, list);
23+
helper(node.left, sum - node.val, result, list);
24+
helper(node.right, sum - node.val, result, list);
2625
}
2726

2827
list.remove(list.size() - 1);

leetcode/src/Main.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,27 @@ public class Main {
66

77
public static class Solution {
88

9-
public int sumOfLeftLeaves(TreeNode root) {
9+
public List<List<Integer>> pathSum(TreeNode root, int sum) {
10+
List<List<Integer>> result = new ArrayList<>();
11+
helpr(root, sum, new ArrayList<>(), result);
12+
return result;
13+
}
14+
15+
private void helpr(TreeNode root, int sum, List<Integer> list, List<List<Integer>> result) {
16+
if (root == null) {
17+
return;
18+
}
19+
20+
list.add(root.val);
21+
22+
if (root.left == null && root.right == null && root.val == sum) {
23+
result.add(new ArrayList<>(list));
24+
} else {
25+
helpr(root.left, sum - root.val, list, result);
26+
helpr(root.right, sum - root.val, list, result);
27+
}
1028

29+
list.remove(list.size() - 1);
1130
}
1231
}
1332

0 commit comments

Comments
 (0)