forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRootToLeafPathSum.java
More file actions
45 lines (34 loc) · 981 Bytes
/
RootToLeafPathSum.java
File metadata and controls
45 lines (34 loc) · 981 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
42
43
44
45
package Trees;
import java.util.ArrayList;
/**
* Author - archit.s
* Date - 08/11/18
* Time - 11:15 PM
*/
public class RootToLeafPathSum {
public void helper(TreeNode A, int B, ArrayList<Integer> t, ArrayList<ArrayList<Integer>> r ){
if(B == 0 && A.left == null && A.right == null){
r.add(new ArrayList<>(t));
}
if(A.left!=null){
t.add(A.left.val);
helper(A.left, B-A.left.val,t,r);
t.remove(t.size()-1);
}
if(A.right!=null){
t.add(A.right.val);
helper(A.right, B - A.right.val, t,r);
t.remove(t.size()-1);
}
}
public ArrayList<ArrayList<Integer>> pathSum(TreeNode A, int B) {
ArrayList<ArrayList<Integer>> r = new ArrayList<>();
if(A == null){
return r;
}
ArrayList<Integer> t = new ArrayList<>();
t.add(A.val);
helper(A,B-A.val,t,r);
return r;
}
}