-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathPath Sum II.java
26 lines (25 loc) · 943 Bytes
/
Path Sum II.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
class Solution
{
public List<List<Integer>> pathSum(TreeNode root, int targetSum)
{
List<List<Integer>> ans = new ArrayList<>();
pathSum(root, targetSum, new ArrayList<>(), ans);
return ans;
}
public void pathSum(TreeNode root, int targetSum, List<Integer> path, List<List<Integer>> ans)
{
if(root == null)
return;
path.add(root.val);
if(root.left == null && root.right == null && targetSum == root.val)//leaf node that completes path
{
ans.add(new ArrayList(path));// we use new ArrayList because if we don't the originaly List is added which is mutable, if we add a copy that's not mutable.
}
else
{
pathSum(root.left, targetSum-root.val, path, ans);
pathSum(root.right, targetSum-root.val, path, ans);
}
path.remove(path.size()-1); //removal of redundant nodes
}
}