-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.java
53 lines (47 loc) · 1.64 KB
/
Solution.java
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
package _101;
import structure.TreeNode;
import java.util.LinkedList;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/10/09
* desc :
* </pre>
*/
public class Solution {
// public boolean isSymmetric(TreeNode root) {
// return root == null || helper(root.left, root.right);
// }
//
// private boolean helper(TreeNode left, TreeNode right) {
// if (left == null || right == null) return left == right;
// if (left.val != right.val) return false;
// return helper(left.left, right.right) && helper(left.right, right.left);
// }
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
LinkedList<TreeNode> q = new LinkedList<>();
q.add(root.left);
q.add(root.right);
TreeNode left, right;
while (q.size() > 1) {
left = q.pop();
right = q.pop();
if (left == null && right == null) continue;
if (left == null || right == null) return false;
if (left.val != right.val) return false;
q.add(left.left);
q.add(right.right);
q.add(left.right);
q.add(right.left);
}
return true;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.isSymmetric(TreeNode.createTestData("[1,2,2,3,4,4,3]")));
System.out.println(solution.isSymmetric(TreeNode.createTestData("[1,2,2,null,3,null,3]")));
System.out.println(solution.isSymmetric(TreeNode.createTestData("[9,-42,-42,null,76,76,null,null,13,null,13]")));
}
}