/
S24.java
50 lines (46 loc) · 1.53 KB
/
S24.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package S21_30;
import PublicClass.TreeNode;
import PublicClass.Utils;
import java.util.ArrayList;
/**
* 二叉树中和为某一值的路径
* Created by Elvis on 2019/12/23.
*/
public class S24 {
public static void main(String[] args) {
TreeNode root = new TreeNode(10);
TreeNode n1 = new TreeNode(5);
TreeNode n2 = new TreeNode(12);
TreeNode n3 = new TreeNode(4);
TreeNode n4 = new TreeNode(7);
root.left = n1;
root.right = n2;
n1.left = n3;
n1.right = n4;
Utils.printTree(root);
ArrayList<ArrayList<Integer>> list = FindPath(root, 22);
for (ArrayList<Integer> i : list) {
Utils.printArrayList(i);
System.out.println();
}
}
public static ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
public static ArrayList<Integer> list = new ArrayList<>();
public static ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) {
tranverse(root, target);
return result;
}
public static void tranverse(TreeNode root, int target) {
if (root == null) {
return;
}
list.add(root.val);
if (target == root.val && root.left == null && root.right == null) {
result.add(new ArrayList<>(list));
}
FindPath(root.left, target - root.val);
FindPath(root.right, target - root.val);
list.remove(list.size() - 1);
return;
}
}