Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

 

 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7 
 8 class Solution(object):
 9     def isBalanced(self, root):
10         """
11         :type root: TreeNode
12         :rtype: bool
13         """
14         def check(root):
15             if root is None:
16                 return 0
17             left  = check(root.left)
18             right = check(root.right)
19             if left == -1 or right == -1 or abs(left - right) > 1:
20                 return -1
21             return 1 + max(left, right)
22             
23         return check(root) != -1

 ????????

posted on 2017-03-16 17:25  Ci_pea  阅读(163)  评论(0编辑  收藏  举报