Skip to content

Commit a764c71

Browse files
refactor 113
1 parent 2baa306 commit a764c71

File tree

2 files changed

+46
-43
lines changed

2 files changed

+46
-43
lines changed

src/main/java/com/fishercoder/solutions/_113.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8-
/**113. Path Sum II
9-
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
8+
/**
9+
* 113. Path Sum II
10+
11+
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
1012
1113
For example:
1214
Given the below binary tree and sum = 22,
@@ -17,38 +19,40 @@
1719
11 13 4
1820
/ \ / \
1921
7 2 5 1
20-
return
22+
23+
return
2124
[
2225
[5,4,11,2],
2326
[5,8,4,5]
2427
]
2528
*/
2629
public class _113 {
2730

31+
public static class Solution1 {
2832
public List<List<Integer>> pathSum(TreeNode root, int sum) {
29-
List<List<Integer>> allPaths = new ArrayList();
30-
if (root == null) {
31-
return allPaths;
32-
}
33-
dfs(root, new ArrayList(), allPaths, sum);
33+
List<List<Integer>> allPaths = new ArrayList();
34+
if (root == null) {
3435
return allPaths;
36+
}
37+
dfs(root, new ArrayList(), allPaths, sum);
38+
return allPaths;
3539
}
3640

37-
3841
private void dfs(TreeNode root, List<Integer> path, List<List<Integer>> allPaths, int sum) {
39-
path.add(root.val);
40-
if (root.left != null) {
41-
dfs(root.left, path, allPaths, sum - root.val);
42-
}
43-
if (root.right != null) {
44-
dfs(root.right, path, allPaths, sum - root.val);
45-
}
46-
if (root.left == null && root.right == null) {
47-
/**Check if sum equals root.val, not sum equals zero!*/
48-
if (sum == root.val) {
49-
allPaths.add(new ArrayList(path));
50-
}
42+
path.add(root.val);
43+
if (root.left != null) {
44+
dfs(root.left, path, allPaths, sum - root.val);
45+
}
46+
if (root.right != null) {
47+
dfs(root.right, path, allPaths, sum - root.val);
48+
}
49+
if (root.left == null && root.right == null) {
50+
/**Check if sum equals root.val, not sum equals zero!*/
51+
if (sum == root.val) {
52+
allPaths.add(new ArrayList(path));
5153
}
52-
path.remove(path.size() - 1);
54+
}
55+
path.remove(path.size() - 1);
5356
}
54-
}
57+
}
58+
}

src/test/java/com/fishercoder/_113Test.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,24 @@
1313
import static org.junit.Assert.assertEquals;
1414

1515
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;
16+
private static _113.Solution1 solution1;
17+
private static TreeNode root;
18+
private static int sum;
19+
private static List<List<Integer>> expected;
2020

21-
@BeforeClass
22-
public static void setup() {
23-
test = new _113();
24-
}
21+
@BeforeClass
22+
public static void setup() {
23+
solution1 = new _113.Solution1();
24+
}
2525

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-
}
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, solution1.pathSum(root, sum));
35+
}
36+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy