to debug:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param root TreeNode类
# @return bool布尔型
#
from typing import Tuple
class Solution:
def isCompleteTree(self , root: TreeNode) -> bool:
depth = 1
choices = [root]

def next_loop(choices:List[TreeNode])->Tuple[List[TreeNode],bool]:
new_choices = []
null_leaves = 0
for choice in choices:
if choice.left is not None:
if null_leaves==0:
new_choices.append(choice.left)
else:
return [],False
else:
null_leaves+=1
if choice.right is not None:
if null_leaves==0:
new_choices.append(choice.right)
else:
return [],False
else:
null_leaves+=1
print(new_choices)
return new_choices,True

while len(choices)!=0:
new_choices,not_all_left = next_loop(choices)
if (not not_all_left):
return False
if len(new_choices)!=0 and len(new_choices)!=2**(depth-1):
return False
depth+=1
choices = new_choices
return True