Skip to content

Commit 2ff0c9a

Browse files
refactor 113
1 parent 2fde25d commit 2ff0c9a

File tree

3 files changed

+43
-54
lines changed

3 files changed

+43
-54
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ Your ideas/fixes/algorithms are more than welcome!
495495
|116|[Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_116.java)| O(n)|O(1) | Medium| BFS
496496
|115|[Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_115.java)| O(m*n)|O(m*n) | Hard| DP
497497
|114|[Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_114.java)| O(n)|O(h) | Medium| Tree
498-
|113|[Path Sum II](https://leetcode.com/problems/path-sum-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_113.java)| O(n)|O(h) | Medium| DFS
498+
|113|[Path Sum II](https://leetcode.com/problems/path-sum-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_113.java)| O(n)|O(h) | Medium| DFS, Backtracking
499499
|112|[Path Sum](https://leetcode.com/problems/path-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_112.java)| O(n)|O(1) | Easy| DFS
500500
|111|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_111.java)| O(n)|O(1)~O(h) | Easy| BFS, DFS
501501
|110|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_110.java)| O(n)|O(1)~O(h) | Easy| DFS
Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.fishercoder.solutions;
22

33
import com.fishercoder.common.classes.TreeNode;
4-
import com.fishercoder.common.utils.CommonUtils;
54

65
import java.util.ArrayList;
76
import java.util.List;
@@ -25,14 +24,13 @@
2524
]
2625
*/
2726
public class _113 {
28-
//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
27+
2928
public List<List<Integer>> pathSum(TreeNode root, int sum) {
3029
List<List<Integer>> allPaths = new ArrayList();
3130
if (root == null) {
3231
return allPaths;
3332
}
34-
List<Integer> path = new ArrayList();
35-
dfs(root, path, allPaths, sum);
33+
dfs(root, new ArrayList(), allPaths, sum);
3634
return allPaths;
3735
}
3836

@@ -46,57 +44,11 @@ private void dfs(TreeNode root, List<Integer> path, List<List<Integer>> allPaths
4644
dfs(root.right, path, allPaths, sum - root.val);
4745
}
4846
if (root.left == null && root.right == null) {
47+
/**Check if sum equals root.val, not sum equals zero!*/
4948
if (sum == root.val) {
50-
List<Integer> onePath = new ArrayList(path);
51-
allPaths.add(onePath);
49+
allPaths.add(new ArrayList(path));
5250
}
5351
}
5452
path.remove(path.size() - 1);
5553
}
56-
57-
58-
public static void main(String... strings) {
59-
_113 test = new _113();
60-
// TreeNode root = new TreeNode(1);
61-
// root.left = new TreeNode(2);
62-
// int sum = 1;
63-
64-
// TreeNode root = new TreeNode(1);
65-
// root.left = new TreeNode(-2);
66-
// root.left.left = new TreeNode(1);
67-
// root.left.right = new TreeNode(3);
68-
// root.right = new TreeNode(-3);
69-
// root.right.left = new TreeNode(-2);
70-
// root.left.left.left = new TreeNode(-1);
71-
// int sum = 2;
72-
// 1
73-
// / \
74-
// -2 -3
75-
// / \ /
76-
// 1 3 -2
77-
// /
78-
// -1
79-
80-
TreeNode root = new TreeNode(5);
81-
root.left = new TreeNode(4);
82-
root.left.left = new TreeNode(11);
83-
root.left.left.left = new TreeNode(7);
84-
root.left.left.right = new TreeNode(2);
85-
root.right = new TreeNode(8);
86-
root.right.left = new TreeNode(13);
87-
root.right.right = new TreeNode(4);
88-
root.right.right.left = new TreeNode(5);
89-
root.right.right.right = new TreeNode(1);
90-
int sum = 22;
91-
// 5
92-
// / \
93-
// 4 8
94-
// / / \
95-
// 11 13 4
96-
// / \ / \
97-
// 7 2 5 1
98-
List<List<Integer>> res = test.pathSum(root, sum);
99-
CommonUtils.printListList(res);
100-
}
101-
102-
}
54+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._113;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
import static org.junit.Assert.assertEquals;
14+
15+
public class _113Test {
16+
private static _113 test;
17+
private static TreeNode root;
18+
private static int sum;
19+
private static List<List<Integer>> expected;
20+
21+
@BeforeClass
22+
public static void setup() {
23+
test = new _113();
24+
}
25+
26+
@Test
27+
public void test1() {
28+
sum = 22;
29+
root = TreeUtils.constructBinaryTree(Arrays.asList(5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1));
30+
TreeUtils.printBinaryTree(root);
31+
expected = new ArrayList<>();
32+
expected.add(Arrays.asList(5, 4, 11, 2));
33+
expected.add(Arrays.asList(5, 8, 4, 5));
34+
assertEquals(expected, test.pathSum(root, sum));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)