-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
44 lines (42 loc) · 1.15 KB
/
Solution.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
37
38
39
40
41
42
43
44
/**
* Definition for binary tree
* class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) {
* val = x;
* left=null;
* right=null;
* }
* }
*/
public class Solution {
public ArrayList<ArrayList<Integer>> pathSum(TreeNode A, int B) {
ArrayList<ArrayList<Integer>> sum=new ArrayList<>();
if(A==null)
return sum;
ArrayList<Integer> l=new ArrayList<>();
l.add(A.val);
sumReturn(l,B-A.val,A,sum);
return sum;
}
public void sumReturn(ArrayList<Integer> l, int B, TreeNode A, ArrayList<ArrayList<Integer>> sum){
if(B==0 && A.left==null && A.right==null){
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.addAll(l);
sum.add(temp);
return;
}
if(A.left!=null){
l.add(A.left.val);
sumReturn(l,B-A.left.val,A.left,sum);
l.remove(l.size()-1);
}
if(A.right!=null){
l.add(A.right.val);
sumReturn(l,B-A.right.val,A.right,sum);
l.remove(l.size()-1);
}
}
}