# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param root TreeNode类 
# @return bool布尔型
#
class Solution:
    def isCompleteTree(self , root: TreeNode) -> bool:
        # write code here
        if not root: return True 
        s = [root]
        flag = False # Check if encounter None
        while s:
            n = len(s)
            for _ in range(n):
                pop = s.pop(0)
                if flag and pop is not None: # encounter None twice
                    return False
                if pop is None: 
                    flag = True 
                else:
                    s.append(pop.left)
                    s.append(pop.right)

        return True