-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFindAllPathsWithGivenSum.java
36 lines (34 loc) · 1.15 KB
/
FindAllPathsWithGivenSum.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
/*https://leetcode.com/problems/path-sum-ii/*/
class Solution
{
List<List<Integer>> result;
public List<List<Integer>> pathSum(TreeNode root, int targetSum)
{
result = new ArrayList<List<Integer>>();
List<Integer> currList = new ArrayList<Integer>();
traversePreOrder(root,0,targetSum,currList);
return result;
}
void traversePreOrder(TreeNode root, int sum, int targetSum, List<Integer> currList)
{
if (root != null)
{
if (root.left == null && root.right == null)
{
if (sum+root.val == targetSum)
{
List<Integer> temp = new ArrayList<Integer>();
for (Integer elem : currList)
temp.add(elem);
temp.add(root.val);
result.add(temp);
}
return;
}
currList.add(root.val);
traversePreOrder(root.left,sum+root.val,targetSum,currList);
traversePreOrder(root.right,sum+root.val,targetSum,currList);
currList.remove(currList.size()-1);
}
}
}