================================ 958. 二叉树的完全性检验 ================================ https://leetcode-cn.com/problems/check-completeness-of-a-binary-tree/ 给定一个二叉树,确定它是否是一个完全二叉树。 百度百科中对完全二叉树的定义如下: 若设二叉树的深度为 h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。(注:第 h 层可能包含 1~ 2h 个节点。)   示例 1:: 输入:[1,2,3,4,5,6] 输出:true 解释:最后一层前的每一层都是满的(即,结点值为 {1} 和 {2,3} 的两层),且最后一层中的所有结点({4,5,6})都尽可能地向左 .. note:: - 如果有空节点,只可能在最后一层,且在左边 .. code:: python # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def isCompleteTree(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True stack = [root] flag = False while stack: # 注意,要弹左边的 node = stack.pop(0) if not node: flag = True continue if flag: return False stack.append(node.left) stack.append(node.right) return True