-
Notifications
You must be signed in to change notification settings - Fork 0
/
113.java
29 lines (29 loc) · 933 Bytes
/
113.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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> result= new LinkedList<List<Integer>>();
if(root==null) return result;
if(root.left==null&&root.right==null&&sum==root.val){
List<Integer> temp = new LinkedList<Integer>();
temp.add(root.val);
result.add(temp);
return result;
}
result = pathSum(root.left, sum-root.val);
for(List<Integer> t : result) ((LinkedList)t).addFirst(root.val);
List<List<Integer>> right= pathSum(root.right, sum-root.val);
for(List<Integer> t : right){
((LinkedList)t).addFirst(root.val);
result.add(t);
}
return result;
}
}