forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_113.java
65 lines (58 loc) · 2.25 KB
/
_113.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;
public class _113 {
public static class Solution1 {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> allPaths = new ArrayList();
if (root == null) {
return allPaths;
}
dfs(root, new ArrayList(), allPaths, sum);
return allPaths;
}
private void dfs(TreeNode root, List<Integer> path, List<List<Integer>> allPaths, int sum) {
path.add(root.val);
if (root.left != null) {
dfs(root.left, path, allPaths, sum - root.val);
}
if (root.right != null) {
dfs(root.right, path, allPaths, sum - root.val);
}
if (root.left == null && root.right == null) {
/**Check if sum equals root.val, not sum equals zero!*/
if (sum == root.val) {
allPaths.add(new ArrayList(path));
}
}
path.remove(path.size() - 1);
}
}
public static class Solution2 {
/**
* My completely original solution on 10/27/2021.
* A classic backtracking problem/solution.
*/
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> ans = new ArrayList<>();
backtracking(root, new ArrayList<>(), targetSum, 0, ans);
return ans;
}
private void backtracking(TreeNode root, List<Integer> path, int targetSum, int currentSum, List<List<Integer>> ans) {
if (root == null) {
return;
}
path.add(root.val);
currentSum += root.val;
if (currentSum == targetSum && root.left == null && root.right == null) {
ans.add(new ArrayList<>(path));
path.remove(path.size() - 1);//backtracking
return;
}
backtracking(root.left, path, targetSum, currentSum, ans);
backtracking(root.right, path, targetSum, currentSum, ans);
path.remove(path.size() - 1);//backtracking
}
}
}