-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path113-pathSum!!!!.java
More file actions
32 lines (31 loc) · 1.05 KB
/
113-pathSum!!!!.java
File metadata and controls
32 lines (31 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
// 回溯法的利用
class Solution_113 {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int sum) {
compare(root, sum, new ArrayList<Integer>());
return res;
}
public void compare(TreeNode root, int sum, List<Integer> list) {
if(root == null) return;
list.add(root.val);
if(root.left==null && root.right==null) {
if(root.val == sum) {
// 这里注意需要新建一个对象,否则后面指向会被清除
res.add(new ArrayList<>(list));
}
}
compare(root.left, sum-root.val, list); //左边结束后,如果返回,就把list左边的节点删除,之后再继续
compare(root.right, sum-root.val, list);
list.remove(list.size() - 1); //将访问的叶子节点删除
}
}