-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution34.java
More file actions
30 lines (26 loc) · 809 Bytes
/
Copy pathsolution34.java
File metadata and controls
30 lines (26 loc) · 809 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
package nowcoder;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class solution34 {
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) {
backtracking(root,target,new ArrayList<>());
return ret;
}
private void backtracking(TreeNode node, int target, ArrayList<Integer> path) {
if(node == null)
{
return;
}
path.add(node.val);
target -= node.val;
if(target==0&&node.left==null&&node.right==null)
{
ret.add(new ArrayList<>(path));
}else{
backtracking(node.left,target,path);
backtracking(node.right,target,path);
}
path.remove(path.size()-1);
}
}