using System;
using System.Collections.Generic;

/*
public class TreeNode
{
	public int val;
	public TreeNode left;
	public TreeNode right;

	public TreeNode (int x)
	{
		val = x;
	}
}
*/

class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return bool布尔型
     */
    public bool isCompleteTree (TreeNode root) {
        if(root == null) return true;
        Queue<TreeNode> que = new Queue<TreeNode>();
        que.Enqueue(root);
        TreeNode cur;
        bool isEnd = false;
        while(!isEnd){
            for(int i = 0; i < que.Count; i++){
                cur = que.Dequeue();
                if(cur == null){
                    if(!isEnd) isEnd = true;
                    continue;
                }
                else{
                    if(isEnd) return false;
                    que.Enqueue(cur.left);
                    que.Enqueue(cur.right);
                }
            }
        }
        for(int i = 0; i < que.Count; i++){
            cur = que.Dequeue();
            if(cur == null){
                if(!isEnd) isEnd = true;
                continue;
            }
            else{
                if(isEnd) return false;
                que.Enqueue(cur.left);
                que.Enqueue(cur.right);
            }
        }
        return true;
    }
}