from collections import deque
class Solution:
def judgeIt(self , root ):
# write code here
# 搜索二叉树,中序遍历递增
# 完全二叉树,序号和满二叉树一致,叶子节点可以是左孩子,不能只有右孩子
if not root:
return [True, True]
out = []
self.num = 0
result = []
def dfs(root):
if result:
return
if not root:
return
dfs(root.left)
if result: # 注意哈不只是开头,这里也要加,因为可能执行了第一个
return
out.append(root.val)
if self.num > 0:
if out[-1] < out[-2]:
# if len(result) == 0:
result.append(False)
return
self.num += 1
dfs(root.right)
dfs(root)
if not result:
result.append(True)
queue = deque()
queue.append(root)
def level(root):
sign = 0
while queue:
res = queue.popleft()
if res.left:
if sign:
result.append(False)
return result
queue.append(res.left)
else:
sign = 1
if res.right:
result.append(False)
return result
if res.right:
if sign:
result.append(False)
return result
queue.append(res.right)
else:
sign = 1
level(root)
if len(result) == 1:
result.append(True)
return result