10000 refactor 101 · matrix-revolution/Leetcode@ffa9e52 · GitHub
[go: up one dir, main page]

Skip to content

Commit ffa9e52

Browse files
refactor 101
1 parent eef4c55 commit ffa9e52

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,26 @@ Given a binary tree, check whether it is a mirror of itself (ie, symmetric aroun
2323
3 3
2424
2525
Note:
26-
Bonus points if you could solve it both recursively and iteratively. */
26+
Bonus points if you could solve it both recursively and iteratively.
27+
*/
28+
2729
public class _101 {
28-
public boolean isSymmetric(TreeNode root) {
29-
if (root == null) {
30-
return true;
30+
public static class Solution1 {
31+
public boolean isSymmetric(TreeNode root) {
32+
if (root == null) {
33+
return true;
34+
}
35+
return isSymmetric(root.left, root.right);
3136
}
32-
return isSymmetric(root.left, root.right);
33-
}
3437

35-
private boolean isSymmetric(TreeNode left, TreeNode right) {
36-
if (left == null || right == null) {
37-
return left == right;
38-
}
39-
if (left.val != right.val) {
40-
return false;
38+
private boolean isSymmetric(TreeNode left, TreeNode right) {
39+
if (left == null || right == null) {
40+
return left == right;
41+
}
42+
if (left.val != right.val) {
43+
return false;
44+
}
45+
return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
4146
}
42-
return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
4347
}
4448
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._101;
6+
import java.util.Arrays;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _101Test {
13+
private static _101.Solution1 solution1;
14+
private static TreeNode root;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _101.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 2, 3, 4, 4, 3));
24+
assertEquals(true, solution1.isSymmetric(root));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 2, null, 3, null, 3));
30+
assertEquals(false, solution1.isSymmetric(root));
31+
}
32+
}

0 commit comments

Comments
 (0)
0