forked from algorhythms/LeetCode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
35 lines (30 loc) · 849 Bytes
/
Solution.java
File metadata and controls
35 lines (30 loc) · 849 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
package PathSumII;
import commons.datastructures.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* User: Danyang
* Date: 1/19/2015
* Time: 19:51
*/
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> ret = new ArrayList<>();
dfs(root, new ArrayList<>(), sum, ret);
return ret;
}
void dfs(TreeNode c, List<Integer> cur, int remain, List<List<Integer>> ret) {
if(c==null)
return;
remain -= c.val;
cur.add(c.val);
if(remain==0 && c.left==null && c.right==null) {
List<Integer> t = new ArrayList<>();
t.addAll(cur);
ret.add(t);
}
dfs(c.left, cur, remain, ret);
dfs(c.right, cur, remain, ret);
cur.remove(cur.size()-1);
}
}