Skip to content

Latest commit

 

History

History
30 lines (26 loc) · 762 Bytes

110.md

File metadata and controls

30 lines (26 loc) · 762 Bytes
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def isBalanced(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if root == None:
            return True
        elif abs(self.Height(root.left) - self.Height(root.right)) <= 1:
            return self.isBalanced(root.left) and self.isBalanced(root.right)
        else:
            return False
        
    
    def Height(self, root):
        if root == None:
            return 0
        else:
            return max(self.Height(root.left), self.Height(root.right)) + 1

深度 平衡二叉树 递归