-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
41 lines (35 loc) · 957 Bytes
/
Copy pathSolution.java
File metadata and controls
41 lines (35 loc) · 957 Bytes
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
39
40
41
import java.util.*;
public class Solution
{
public List<List<Integer>> pathSum(TreeNode root, int sum)
{
List<List<Integer>> paths = new ArrayList<>();
getPaths(root, sum, new ArrayList<Integer>(), paths);
return paths;
}
private void getPaths(TreeNode root, int sum, ArrayList<Integer> arrayList, List<List<Integer>> paths)
{
if (root == null)
{
return;
}
arrayList.add(root.val);
if (sum == root.val && root.left == null && root.right == null)
{
paths.add(arrayList);
return;
}
getPaths(root.left, sum - root.val, new ArrayList<>(arrayList), paths);
getPaths(root.right, sum - root.val, new ArrayList<>(arrayList), paths);
}
public class TreeNode
{
int val;
TreeNode left;
TreeNode right;
TreeNode(int x)
{
val = x;
}
}
}