-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPathsFromRootWithASpecifiedSum.java
34 lines (31 loc) · 1.11 KB
/
PathsFromRootWithASpecifiedSum.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
/*https://practice.geeksforgeeks.org/problems/paths-from-root-with-a-specified-sum/1*/
class Solution
{
static ArrayList<ArrayList<Integer>> result;
public static ArrayList<ArrayList<Integer>> printPaths(Node root, int sum)
{
//code here
result = new ArrayList<ArrayList<Integer>>();
traversePreOrder(root, sum, 0, new ArrayList<Integer>());
return result;
}
public static void traversePreOrder(Node root, int sum, int currSum, ArrayList<Integer> currPath)
{
if (root != null)
{
currSum += root.data;
currPath.add(root.data);
if (currSum == sum)
{
ArrayList<Integer> newList = new ArrayList<Integer>();
for (Integer element : currPath)
newList.add(element);
result.add(newList);
}
traversePreOrder(root.left, sum, currSum, currPath);
traversePreOrder(root.right, sum, currSum, currPath);
currSum -= root.data;
currPath.remove(currPath.size()-1);
}
}
}