|
1 |
| -package com.fishercoder.solutions.firstthousand; |
2 |
| - |
3 |
| -import com.fishercoder.common.classes.TreeNode; |
4 |
| - |
5 |
| -public class _101 { |
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 |
19 |
| - && isSymmetric(left.left, right.right) |
20 |
| - && isSymmetric(left.right, right.left); |
21 |
| - } |
22 |
| - } |
23 |
| - |
24 |
| - public static class Solution2 { |
25 |
| - /* |
26 |
| - * The same as the above solution, just a bit more verbose. |
27 |
| - */ |
28 |
| - public boolean isSymmetric(TreeNode root) { |
29 |
| - if (root == null) { |
30 |
| - return true; |
31 |
| - } |
32 |
| - return isSymmetric(root.left, root.right); |
33 |
| - } |
34 |
| - |
35 |
| - private boolean isSymmetric(TreeNode left, TreeNode right) { |
36 |
| - if (left == null && right == null) { |
37 |
| - return true; |
38 |
| - } else if (left == null || right == null) { |
39 |
| - return false; |
40 |
| - } |
41 |
| - if (left.val == right.val) { |
42 |
| - return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left); |
43 |
| - } else { |
44 |
| - return false; |
45 |
| - } |
46 |
| - } |
47 |
| - } |
48 |
| -} |
| 1 | +package com.fishercoder.solutions.firstthousand; |
| 2 | + |
| 3 | +import com.fishercoder.common.classes.TreeNode; |
| 4 | + |
| 5 | +public class _101 { |
| 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 |
| 19 | + && isSymmetric(left.left, right.right) |
| 20 | + && isSymmetric(left.right, right.left); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + public static class Solution2 { |
| 25 | + /* |
| 26 | + * The same as the above solution, just a bit more verbose. |
| 27 | + */ |
| 28 | + public boolean isSymmetric(TreeNode root) { |
| 29 | + if (root == null) { |
| 30 | + return true; |
| 31 | + } |
| 32 | + return isSymmetric(root.left, root.right); |
| 33 | + } |
| 34 | + |
| 35 | + private boolean isSymmetric(TreeNode left, TreeNode right) { |
| 36 | + if (left == null && right == null) { |
| 37 | + return true; |
| 38 | + } else if (left == null || right == null) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + if (left.val == right.val) { |
| 42 | + return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left); |
| 43 | + } else { |
| 44 | + return false; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments