-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path101. Symmetric Tree.java
55 lines (54 loc) · 2.29 KB
/
101. Symmetric Tree.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
54
55
________________________________________________________Best Solution__________________________________________________________
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
return helper(root.left,root.right);
}
// compare l.l, r.r && l.r, r.l recursively
private boolean helper(TreeNode left, TreeNode right) {
if(left == null && right == null) return true;
if(left != null && right != null && left.val == right.val)
// compare l.l with r.r and l.r with r.l
return helper(left.left, right.right) && helper(left.right,right.left);
else return false;
}
}
________________________________________________________My Solution____________________________________________________________
class Solution {
public boolean isSymmetric(TreeNode root) {
// null
if(root == null) return true;
// only has root node
if(root.left == null && root.right == null) return true;
// only one of them is null
if((root.left != null && root.right == null)
|| (root.right != null && root.left == null)) return false;
// sencond layer is not symmetric
if(root.left.val != root.right.val) return false;
// record left tree
Deque<TreeNode> leftRec = new LinkedList();
// record right tree
Deque<TreeNode> rigtRec = new LinkedList();
leftRec.offer(root.left);
rigtRec.offer(root.right);
while(!leftRec.isEmpty()){
// l retrive from head
TreeNode l = leftRec.poll();
// r retrive from tail
TreeNode r = rigtRec.pollLast();
if(l == null) continue;
if(r.val != l.val) return false;
// current pair match with each other, l.l r.r
if(l.left != null && r.right != null || (l.left == null && r.right == null)){
leftRec.offer(l.left);
rigtRec.push(r.right);
}else return false;
//current pair match with each other, l.r r.l
if(l.right != null && r.left != null || (r.left == null && l.right == null)){
leftRec.offer(l.right);
rigtRec.push(r.left);
}else return false;
}
return true;
}
}