Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5b7b8b4

Browse files
committedSep 23, 2021
refactor 101
1 parent 3f164ff commit 5b7b8b4

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed
 

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

+25
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,29 @@ private boolean isSymmetric(TreeNode left, TreeNode right) {
1818
return left.val == right.val && isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
1919
}
2020
}
21+
22+
public static class Solution2 {
23+
/**
24+
* The same as the above solution, just a bit more verbose.
25+
*/
26+
public boolean isSymmetric(TreeNode root) {
27+
if (root == null) {
28+
return true;
29+
}
30+
return isSymmetric(root.left, root.right);
31+
}
32+
33+
private boolean isSymmetric(TreeNode left, TreeNode right) {
34+
if (left == null && right == null) {
35+
return true;
36+
} else if (left == null || right == null) {
37+
return false;
38+
}
39+
if (left.val == right.val) {
40+
return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
41+
} else {
42+
return false;
43+
}
44+
}
45+
}
2146
}

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

+25-19
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,36 @@
33
import com.fishercoder.common.classes.TreeNode;
44
import com.fishercoder.common.utils.TreeUtils;
55
import com.fishercoder.solutions._101;
6+
67
import java.util.Arrays;
8+
79
import org.junit.BeforeClass;
810
import org.junit.Test;
911

1012
import static org.junit.Assert.assertEquals;
1113

1214
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-
}
15+
private static _101.Solution1 solution1;
16+
private static _101.Solution2 solution2;
17+
private static TreeNode root;
18+
19+
@BeforeClass
20+
public static void setup() {
21+
solution1 = new _101.Solution1();
22+
solution2 = new _101.Solution2();
23+
}
24+
25+
@Test
26+
public void test1() {
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+
public void test2() {
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)
Failed to load comments.