forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_113.java
37 lines (32 loc) · 1.15 KB
/
_113.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;
public class _113 {
public static class Solution1 {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> allPaths = new ArrayList();
if (root == null) {
return allPaths;
}
dfs(root, new ArrayList(), allPaths, sum);
return allPaths;
}
private void dfs(TreeNode root, List<Integer> path, List<List<Integer>> allPaths, int sum) {
path.add(root.val);
if (root.left != null) {
dfs(root.left, path, allPaths, sum - root.val);
}
if (root.right != null) {
dfs(root.right, path, allPaths, sum - root.val);
}
if (root.left == null && root.right == null) {
/**Check if sum equals root.val, not sum equals zero!*/
if (sum == root.val) {
allPaths.add(new ArrayList(path));
}
}
path.remove(path.size() - 1);
}
}
}