核心思路:
1.如果有右节点,没有左节点,直接返回False
2.如果左右节点都没有,或者只有左节点,就看看后面的节点,必须是叶节点,不能有子节点。(就是看看这是不是最后一排。)
class IsCompleteTree:
def iscompltee(self, root):
res = [root]
flag = 0
while res:
cur = res.pop()
if cur.left:
res.append(cur.left)
if cur.right:
res.append(cur.right)
if cur.right and not cur.left:
return False
if flag:
if cur.left and cur.right:
return False
if cur.left and not cur.right or (not cur.left and not cur.right):
flag = 1
return True