-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSymmetricTree.java
More file actions
59 lines (52 loc) · 1.35 KB
/
SymmetricTree.java
File metadata and controls
59 lines (52 loc) · 1.35 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Leetcode;
/**
* @author kalpak
*
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
*
* For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
*
* 1
* / \
* 2 2
* / \ / \
* 3 4 4 3
*
*
* But the following [1,2,2,null,3,null,3] is not:
*
* 1
* / \
* 2 2
* \ \
* 3 3
*
*
* Follow up: Solve it both recursively and iteratively.
*
*/
public class SymmetricTree {
public static boolean isSymmetric(TreeNode root) {
return areMirrorTree(root, root);
}
private static boolean areMirrorTree(TreeNode root1, TreeNode root2) {
if(root1 == null && root2 == null)
return true;
if(root1 == null || root2 == null)
return false;
if(root1.val == root2.val)
return areMirrorTree(root1.left, root2.right) && areMirrorTree(root1.right, root2.left);
else
return false;
}
public static void main(String[] args) {
TreeNode root1 = new TreeNode(1);
root1.left = new TreeNode(2);
root1.right = new TreeNode(2);
root1.left.left = new TreeNode(3);
root1.left.right = new TreeNode(4);
root1.right.left = new TreeNode(4);
root1.right.right = new TreeNode(3);
System.out.println(isSymmetric(root1));
}
}