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

Commit5b7b8b4

Browse files
refactor 101
1 parent3f164ff commit5b7b8b4

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

‎src/main/java/com/fishercoder/solutions/_101.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,29 @@ private boolean isSymmetric(TreeNode left, TreeNode right) {
1818
returnleft.val ==right.val &&isSymmetric(left.left,right.right) &&isSymmetric(left.right,right.left);
1919
}
2020
}
21+
22+
publicstaticclassSolution2 {
23+
/**
24+
* The same as the above solution, just a bit more verbose.
25+
*/
26+
publicbooleanisSymmetric(TreeNoderoot) {
27+
if (root ==null) {
28+
returntrue;
29+
}
30+
returnisSymmetric(root.left,root.right);
31+
}
32+
33+
privatebooleanisSymmetric(TreeNodeleft,TreeNoderight) {
34+
if (left ==null &&right ==null) {
35+
returntrue;
36+
}elseif (left ==null ||right ==null) {
37+
returnfalse;
38+
}
39+
if (left.val ==right.val) {
40+
returnisSymmetric(left.left,right.right) &&isSymmetric(left.right,right.left);
41+
}else {
42+
returnfalse;
43+
}
44+
}
45+
}
2146
}

‎src/test/java/com/fishercoder/_101Test.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,36 @@
33
importcom.fishercoder.common.classes.TreeNode;
44
importcom.fishercoder.common.utils.TreeUtils;
55
importcom.fishercoder.solutions._101;
6+
67
importjava.util.Arrays;
8+
79
importorg.junit.BeforeClass;
810
importorg.junit.Test;
911

1012
importstaticorg.junit.Assert.assertEquals;
1113

1214
publicclass_101Test {
13-
privatestatic_101.Solution1solution1;
14-
privatestaticTreeNoderoot;
15-
16-
@BeforeClass
17-
publicstaticvoidsetup() {
18-
solution1 =new_101.Solution1();
19-
}
20-
21-
@Test
22-
publicvoidtest1() {
23-
root =TreeUtils.constructBinaryTree(Arrays.asList(1,2,2,3,4,4,3));
24-
assertEquals(true,solution1.isSymmetric(root));
25-
}
26-
27-
@Test
28-
publicvoidtest2() {
29-
root =TreeUtils.constructBinaryTree(Arrays.asList(1,2,2,null,3,null,3));
30-
assertEquals(false,solution1.isSymmetric(root));
31-
}
15+
privatestatic_101.Solution1solution1;
16+
privatestatic_101.Solution2solution2;
17+
privatestaticTreeNoderoot;
18+
19+
@BeforeClass
20+
publicstaticvoidsetup() {
21+
solution1 =new_101.Solution1();
22+
solution2 =new_101.Solution2();
23+
}
24+
25+
@Test
26+
publicvoidtest1() {
27+
root =TreeUtils.constructBinaryTree(Arrays.asList(1,2,2,3,4,4,3));
28+
assertEquals(true,solution1.isSymmetric(root));
29+
assertEquals(true,solution2.isSymmetric(root));
30+
}
31+
32+
@Test
33+
publicvoidtest2() {
34+
root =TreeUtils.constructBinaryTree(Arrays.asList(1,2,2,null,3,null,3));
35+
assertEquals(false,solution1.isSymmetric(root));
36+
assertEquals(false,solution2.isSymmetric(root));
37+
}
3238
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp