题解 | #判断是不是完全二叉树#
判断是不是完全二叉树
https://www.nowcoder.com/practice/8daa4dff9e36409abba2adbe413d6fae
# 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 # 主要思想,按层遍历,中间不会有空 queue = [root] while queue: tmp = queue.pop(0) if not tmp: # 如果是None while queue: # 遍历后续的,如果有非None,则返回False tmp2 = queue.pop(0) if tmp2: return False else: queue.append(tmp.left) queue.append(tmp.right) return True