题解 | #对称的二叉树#
对称的二叉树
http://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
import java.util.*;
public class Solution {
boolean isSymmetrical(TreeNode pRoot) {
if(pRoot==null){
return true;
}
Queue<TreeNode> queue = new LinkedList<>();
if(pRoot.left!=null){
queue.add(pRoot.left);
}
if(pRoot.right!=null){
queue.add(pRoot.right);
}
while(!queue.isEmpty()){
int size = queue.size();
if(size%2!=0){
return false;
}
int c = (size-1) >> 1;
Stack<TreeNode> stack = new Stack<>();
for(int i = 0;i<size;i++){
TreeNode poll = queue.poll();
if(i<=c){
stack.add(poll);
}else{
TreeNode pop = stack.pop();
if(poll!=null&&pop!=null){
if(pop.val!=poll.val){
return false;
}
}else if(poll != null || pop != null){
return false;
}
}
if(poll!=null){
queue.add(poll.left);
queue.add(poll.right);
}
}
}
return true;
}
}