Skip to content

Commit 9154689

Browse files
add a solution for 113
1 parent eb460a4 commit 9154689

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,32 @@ private void dfs(TreeNode root, List<Integer> path, List<List<Integer>> allPaths
3434
path.remove(path.size() - 1);
3535
}
3636
}
37+
38+
public static class Solution2 {
39+
/**
40+
* My completely original solution on 10/27/2021.
41+
* A classic backtracking problem/solution.
42+
*/
43+
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
44+
List<List<Integer>> ans = new ArrayList<>();
45+
backtracking(root, new ArrayList<>(), targetSum, 0, ans);
46+
return ans;
47+
}
48+
49+
private void backtracking(TreeNode root, List<Integer> path, int targetSum, int currentSum, List<List<Integer>> ans) {
50+
if (root == null) {
51+
return;
52+
}
53+
path.add(root.val);
54+
currentSum += root.val;
55+
if (currentSum == targetSum && root.left == null && root.right == null) {
56+
ans.add(new ArrayList<>(path));
57+
path.remove(path.size() - 1);//backtracking
58+
return;
59+
}
60+
backtracking(root.left, path, targetSum, currentSum, ans);
61+
backtracking(root.right, path, targetSum, currentSum, ans);
62+
path.remove(path.size() - 1);//backtracking
63+
}
64+
}
3765
}

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

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

1515
public class _113Test {
16-
private static _113.Solution1 solution1;
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 _113.Solution2 solution2;
18+
private static TreeNode root;
19+
private static int sum;
20+
private static List<List<Integer>> expected;
2021

21-
@BeforeClass
22-
public static void setup() {
23-
solution1 = new _113.Solution1();
24-
}
22+
@BeforeClass
23+
public static void setup() {
24+
solution1 = new _113.Solution1();
25+
solution2 = new _113.Solution2();
26+
}
27+
28+
@Test
29+
public void test1() {
30+
sum = 22;
31+
root = TreeUtils.constructBinaryTree(Arrays.asList(5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1));
32+
TreeUtils.printBinaryTree(root);
33+
expected = new ArrayList<>();
34+
expected.add(Arrays.asList(5, 4, 11, 2));
35+
expected.add(Arrays.asList(5, 8, 4, 5));
36+
assertEquals(expected, solution1.pathSum(root, sum));
37+
assertEquals(expected, solution2.pathSum(root, sum));
38+
}
2539

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-
}
3640
}

0 commit comments

Comments
 (0)