-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path113.path-sum-ii.java
More file actions
56 lines (50 loc) · 1.52 KB
/
Copy path113.path-sum-ii.java
File metadata and controls
56 lines (50 loc) · 1.52 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* @lc app=leetcode id=113 lang=java
*
* [113] Path Sum II
*/
// @lc code=start
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// DFS + Backtracking
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> resList = new ArrayList<>();
dfsPathSum(root, sum, resList, new ArrayList());
return resList;
}
private void dfsPathSum(TreeNode root, int sum, List<List<Integer>> resList, List<Integer> currList) {
if (root == null) {
return;
}
// 先把当前的node放进路径结果里
currList.add(root.val);
// 如果当前是leave 并且是正确路径,保存答案
if (root.left == null && root.right == null) {
if (root.val == sum) {
resList.add(new ArrayList<>(currList));
}
}
// 否则dfs找它的左右子节点的路径
else {
dfsPathSum(root.left, sum - root.val, resList, currList);
dfsPathSum(root.right, sum - root.val, resList, currList);
}
//把当前node从路径里移除,还原之前的样子
currList.remove(currList.size() - 1);
}
}
// @lc code=end