Skip to content

Commit 92089c7

Browse files
refactor 101
1 parent 3e76332 commit 92089c7

File tree

1 file changed

+15
-40
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+15
-40
lines changed

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

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,20 @@
22

33
import com.fishercoder.common.classes.TreeNode;
44

5-
/**
6-
* 101. Symmetric Tree
7-
8-
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
9-
10-
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
11-
12-
1
13-
/ \
14-
2 2
15-
/ \ / \
16-
3 4 4 3
17-
18-
But the following [1,2,2,null,3,null,3] is not:
19-
20-
1
21-
/ \
22-
2 2
23-
\ \
24-
3 3
25-
26-
Note:
27-
Bonus points if you could solve it both recursively and iteratively.
28-
*/
29-
305
public class _101 {
31-
public static class Solution1 {
32-
public boolean isSymmetric(TreeNode root) {
33-
if (root == null) {
34-
return true;
35-
}
36-
return isSymmetric(root.left, root.right);
37-
}
38-
39-
private boolean isSymmetric(TreeNode left, TreeNode right) {
40-
if (left == null || right == null) {
41-
return left == right;
42-
}
43-
return left.val == right.val && isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
44-
}
45-
}
6+
public static class Solution1 {
7+
public boolean isSymmetric(TreeNode root) {
8+
if (root == null) {
9+
return true;
10+
}
11+
return isSymmetric(root.left, root.right);
12+
}
13+
14+
private boolean isSymmetric(TreeNode left, TreeNode right) {
15+
if (left == null || right == null) {
16+
return left == right;
17+
}
18+
return left.val == right.val && isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
19+
}
20+
}
4621
}

0 commit comments

Comments
 (0)