二叉树节点个数为N, 时间复杂度为O(N)
def process(head):
#['是否平衡','树的高度']
if head == None:
return [True,0]
left = process(head.left)
right = process(head.right)
height = max(left[1],right[1]) + 1
if left[0] && right[0] && abs(right[1] - left[0]) < 2:
isBanlance = True
else:
isBanlance = False
return [isBanlance,height]
def isBanlance(head):
return process(head)[0]