forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT24.java
More file actions
33 lines (28 loc) · 1.05 KB
/
Copy pathT24.java
File metadata and controls
33 lines (28 loc) · 1.05 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
33
package web; /**
* @program LeetNiu
* @description: 二叉树中和为某一值的路径
* @author: mf
* @create: 2020/01/13 10:43
*/
import java.util.ArrayList;
/**
* 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。
* 路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
* (注意: 在返回值的list中,数组长度大的数组靠前)
*/
public class T24 {
private ArrayList<ArrayList<Integer>> listall = new ArrayList<>();
private ArrayList<Integer> list = new ArrayList<>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
if (root == null) return listall;
list.add(root.val);
target -= root.val;
if (target == 0 && root.left == null && root.right == null) {
listall.add(new ArrayList<>(list));
}
FindPath(root.left, target);
FindPath(root.right, target);
list.remove(list.size() - 1);
return listall;
}
}