-
Notifications
You must be signed in to change notification settings - Fork 740
/
113. Path Sum II.txt
39 lines (33 loc) · 1.07 KB
/
113. Path Sum II.txt
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
Leetcode - 113. Path Sum II
https://leetcode.com/problems/path-sum-ii/
class Solution {
List<List<Integer>> arr=new LinkedList<List<Integer>>();
public List<List<Integer>> pathSum(TreeNode r, int t) {
if(r==null){
return arr;
}
List<Integer> ar=new LinkedList<Integer>();
traversal(r,t,0,ar);
return arr;
}
public void traversal(TreeNode r,int t,int s,List<Integer> ar){
//System.out.println(ar+" "+s+" "+r.val);
if(r.left==null && r.right==null && s+r.val==t){
//System.out.println("Enter"+s);
ar.add(r.val);
arr.add(new LinkedList<Integer>(ar));
ar.remove(ar.size()-1);
return;
}
if(r.left!=null){
ar.add(r.val);
traversal(r.left,t,s+r.val,ar);
ar.remove(ar.size()-1);
}
if(r.right!=null){
ar.add(r.val);
traversal(r.right,t,s+r.val,ar);
ar.remove(ar.size()-1);
}
}
}