题解 | #判断是不是完全二叉树#
判断是不是完全二叉树
https://www.nowcoder.com/practice/8daa4dff9e36409abba2adbe413d6fae
//思路:层次遍历,null也入队,当访问到null时,就把队列中剩余元素全出队,如果其中有非null的,就返回false
public boolean isCompleteTree (TreeNode root) {
// write code here
if(root==null)return true;
Queue<TreeNode>queue=new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int size=queue.size();
for(int i=0;i<size;i++){
TreeNode cur=queue.poll();
if(cur==null){
while(!queue.isEmpty())
if(queue.poll()!=null)
return false;
return true;
}
queue.offer(cur.left);
queue.offer(cur.right);
}
}
return true;
}

