-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLeet113.java
More file actions
32 lines (32 loc) · 1.07 KB
/
Leet113.java
File metadata and controls
32 lines (32 loc) · 1.07 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
31
32
/**
* 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>> list = new ArrayList<>();
helper(root,list,sum,0,"");
return list;
}
public void helper(TreeNode node,List<List<Integer>> list,int target,int sum,String s){
if(node != null){
if(node.left == null && node.right == null){
if(sum+node.val == target){
s = s+node.val;
String[] arr = s.split(",");
List<Integer> item = new ArrayList<>();
for(String num:arr) item.add(Integer.parseInt(num));
list.add(new ArrayList<>(item));
}
} else {
helper(node.left,list,target,sum+node.val,s+node.val+",");
helper(node.right,list,target,sum+node.val,s+node.val+",");
}
}
}
}