forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_113.java
58 lines (50 loc) · 1.35 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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* 113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
*/
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);
}
}
}