subject : Enter a binary tree , Determine whether the binary tree is a balanced binary tree .
Balanced binary trees : Balanced binary search tree , Also known as AVL Trees .
It is an empty tree or its left and right subtrees The absolute value of the height difference does not exceed 1, And both the left and right subtrees are a balanced binary tree .
recursive .
Code :
class Solution:
def __init__(self):
self.balance = True
def find(self , root):
self.func(root)
return self.balance
def func(self , root):
if not root:
return 0
left = self.func(root.left)
right = self.func(root.right)
if abs(left - right) > 1 and self.balance:
self.balance = False
return max(left , right) + 1