def fun1(root):
'''
验证是否为完全二叉树
完全二叉树的定义:
除最后一层外,每一层一定都是满的
最后一层若不满,则为是从左往右依次排列
那么第一想法很简单,不能是深度优先遍历,应该是宽度优先遍历
每一层每一层的遍历,那么不满足什么规则就不是完全二叉树了
1.一个节点只有右子节点,没有左子节点
2.在不违背1的前提下,如果遇到了第一个左右子不全的情况(只有左子节点)后序的均为叶节点
:return:
'''
if not root:
return True
duilie = [root]
panduan=True
while duilie:
root = duilie.pop(0)
if root.right:
if not panduan:
return False
duilie.append(root.right)
if root.left:
duilie.append(root.left)
else:
return False
else:
if panduan:
if root.left:
panduan = False
duilie.append(root.left)
else:
if root.left:
return False
return True
def fun2(root):
'''
找出所有可以证明他不是完全二叉树的判断
1.一个节点只有右子节点,没有左子节点
2.在不违背1的前提下,如果遇到了第一个左右子不全的情况后序的为叶节点
:param root:
:return:
'''
duilie = [root]
panduan=False
while duilie:
root = duilie.pop(0)
l=root.left
r=root.right
if (l==None and r!=None) \
or (panduan and (l!=None or r!=None)):
return False
if l:
duilie.append(root.left)
if r:
duilie.append(root.right)
if (l==None or r==None):
panduan=True
return True