# python--lintcode93. 平衡二叉树

A)  3            B)    3
/ \                  \
9  20                 20
/  \                / \
15   7              15  7

class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None

class Solution:
"""
@param root: The root of binary tree.
@return: True if this Binary tree is Balanced, or false.
"""
def isBalanced(self, root):
def find(root):   #求出树的深度
if (root is None): return 0
left = find(root.left)
right = find(root.right)
if (left > right):
return left + 1
else:
return right + 1

def decide(root):   #判断是否是平衡二叉树
if(root is None):return True
leftDepth=find(root.left)
rightDepth=find(root.right)
if(abs(leftDepth-rightDepth)>1):return False
left=decide(root.left)
right=decide(root.right)
if(left==False or right==False):
return False
return True

result=decide(root)
return result

Tree = TreeNode(2)
Tree.left = TreeNode(1)
aa=Tree.right = TreeNode(3)
aa.right=TreeNode(4)
aa.right.right=TreeNode(5)
s = Solution()
print(s.isBalanced(Tree))