-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSumII.java
More file actions
33 lines (25 loc) · 919 Bytes
/
PathSumII.java
File metadata and controls
33 lines (25 loc) · 919 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
31
32
33
package com.mirraico.leetcode;
import java.util.ArrayList;
import java.util.List;
public class PathSumII {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> ans = new ArrayList<>();
dfs(root, 0, sum, new ArrayList<>(), ans);
return ans;
}
public void dfs(TreeNode root, int tempSum, int sum, List<Integer> tempAns, List<List<Integer>> ans) {
if (null == root) return;
tempSum += root.val;
tempAns.add(root.val);
if (null == root.left && null == root.right) {
if (tempSum == sum) ans.add(new ArrayList<>(tempAns));
tempAns.remove(tempAns.size() - 1);
return;
}
dfs(root.left, tempSum, sum, tempAns, ans);
dfs(root.right, tempSum, sum, tempAns, ans);
tempAns.remove(tempAns.size() - 1);
}
public static void main(String[] args) {
}
}