-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path113. Path Sum II.java
More file actions
30 lines (28 loc) · 1.02 KB
/
113. Path Sum II.java
File metadata and controls
30 lines (28 loc) · 1.02 KB
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> result = new LinkedList<List<Integer>>();
List<Integer> currentResult = new LinkedList<Integer>();
findPathSum(root, sum, currentResult, result);
return result;
}
private void findPathSum(TreeNode root, int sum, List<Integer> currentResult, List<List<Integer>> result){
if(root == null) return;
currentResult.add(root.val);
if(root.left == null && root.right == null && sum == root.val){
result.add(new LinkedList(currentResult));
}else{
findPathSum(root.left, sum - root.val, currentResult, result);
findPathSum(root.right, sum - root.val, currentResult, result);
}
currentResult.remove(currentResult.size() - 1);
}
}