forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSumII.java
More file actions
30 lines (26 loc) · 880 Bytes
/
PathSumII.java
File metadata and controls
30 lines (26 loc) · 880 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
package com.examplehub.leetcode.easy;
import com.examplehub.leetcode.TreeNode;
import java.util.ArrayList;
import java.util.List;
/** https://leetcode.com/problems/path-sum-ii/ */
public class PathSumII {
private static List<List<Integer>> result = new ArrayList<>();
private static List<Integer> path = new ArrayList<>();
public static List<List<Integer>> doSolution1(TreeNode root, int targetNum) {
deepFirstSearch(root, targetNum);
return result;
}
private static void deepFirstSearch(TreeNode root, int targetNum) {
if (root == null) {
return;
}
path.add(root.val);
targetNum -= root.val;
if (root.left == null && root.right == null && targetNum == 0) {
result.add(new ArrayList<>(path));
}
deepFirstSearch(root.left, targetNum);
deepFirstSearch(root.right, targetNum);
path.remove(path.size() - 1);
}
}