Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9154689

Browse files
add a solution for 113
1 parenteb460a4 commit9154689

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+
publicstaticclassSolution2 {
39+
/**
40+
* My completely original solution on 10/27/2021.
41+
* A classic backtracking problem/solution.
42+
*/
43+
publicList<List<Integer>>pathSum(TreeNoderoot,inttargetSum) {
44+
List<List<Integer>>ans =newArrayList<>();
45+
backtracking(root,newArrayList<>(),targetSum,0,ans);
46+
returnans;
47+
}
48+
49+
privatevoidbacktracking(TreeNoderoot,List<Integer>path,inttargetSum,intcurrentSum,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(newArrayList<>(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
importstaticorg.junit.Assert.assertEquals;
1414

1515
publicclass_113Test {
16-
privatestatic_113.Solution1solution1;
17-
privatestaticTreeNoderoot;
18-
privatestaticintsum;
19-
privatestaticList<List<Integer>>expected;
16+
privatestatic_113.Solution1solution1;
17+
privatestatic_113.Solution2solution2;
18+
privatestaticTreeNoderoot;
19+
privatestaticintsum;
20+
privatestaticList<List<Integer>>expected;
2021

21-
@BeforeClass
22-
publicstaticvoidsetup() {
23-
solution1 =new_113.Solution1();
24-
}
22+
@BeforeClass
23+
publicstaticvoidsetup() {
24+
solution1 =new_113.Solution1();
25+
solution2 =new_113.Solution2();
26+
}
27+
28+
@Test
29+
publicvoidtest1() {
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 =newArrayList<>();
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-
publicvoidtest1() {
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 =newArrayList<>();
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)

[8]ページ先頭

©2009-2025 Movatter.jp