在lc上做的移过来了,不是抄的。 双队列,一个从左向右,一个从右向左,注意判断空值。
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.*;
public class Solution {
boolean isSymmetrical(TreeNode pRoot) {
TreeNode root = pRoot;
Queue<TreeNode> left = new LinkedList<>();
Queue<TreeNode> right = new LinkedList<>();
TreeNode lp;
TreeNode rp;
if(root == null){
return true;
}
left.offer(root);
right.offer(root);
int r,l;
while(!left.isEmpty()){
r=0;
l=0;
lp = left.poll();
rp = right.poll();
if(lp.val != rp.val ){
return false;
}
if(lp.left != null){
l =1;
left.offer(lp.left);
}
if(lp.right != null){
r= 1;
left.offer(lp.right);
}
if(rp.right == null && l==1){
return false;
}
if(rp.left == null && r==1){
return false;
}
if(rp.right != null){
right.offer(rp.right);
}
if(rp.left !=null){
right.offer(rp.left);
}
}
return true;
}
}