forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_113.java
100 lines (89 loc) · 2.79 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import com.fishercoder.common.utils.CommonUtils;
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 {
//also, it's possible that a node's value could be negative, as long as the sum of root to leaf ends up to sum
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> allPaths = new ArrayList();
if(root == null) return allPaths;
List<Integer> path = new ArrayList();
dfs(root, path, 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){
if(sum == root.val){
List<Integer> onePath = new ArrayList(path);
allPaths.add(onePath);
}
}
path.remove(path.size()-1);
}
public static void main(String...strings){
_113 test = new _113();
// TreeNode root = new TreeNode(1);
// root.left = new TreeNode(2);
// int sum = 1;
// TreeNode root = new TreeNode(1);
// root.left = new TreeNode(-2);
// root.left.left = new TreeNode(1);
// root.left.right = new TreeNode(3);
// root.right = new TreeNode(-3);
// root.right.left = new TreeNode(-2);
// root.left.left.left = new TreeNode(-1);
// int sum = 2;
// 1
// / \
// -2 -3
// / \ /
// 1 3 -2
// /
// -1
TreeNode root = new TreeNode(5);
root.left = new TreeNode(4);
root.left.left = new TreeNode(11);
root.left.left.left = new TreeNode(7);
root.left.left.right = new TreeNode(2);
root.right = new TreeNode(8);
root.right.left = new TreeNode(13);
root.right.right = new TreeNode(4);
root.right.right.left = new TreeNode(5);
root.right.right.right = new TreeNode(1);
int sum = 22;
// 5
// / \
// 4 8
// / / \
// 11 13 4
// / \ / \
// 7 2 5 1
List<List<Integer>> res = test.pathSum(root, sum);
CommonUtils.printListList(res);
}
}