-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay73.java
More file actions
34 lines (31 loc) · 1.13 KB
/
Day73.java
File metadata and controls
34 lines (31 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class TreeNode {
int val;
TreeNode left, right;
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
}
public class Day73 {
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return isMirror(root.left, root.right);
}
private boolean isMirror(TreeNode left, TreeNode right) {
if (left == null && right == null) return true;
if (left == null || right == null) return false;
return (left.val == right.val) && isMirror(left.left, right.right) && isMirror(left.right, right.left);
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(2);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(4);
root.right.left = new TreeNode(4);
root.right.right = new TreeNode(3);
SymmetricTree symmetricTree = new SymmetricTree();
boolean isSymmetric = symmetricTree.isSymmetric(root);
System.out.println("Is the binary tree symmetric? " + isSymmetric);
}
}