1. # Definition for a binary tree node.
    2. # class TreeNode:
    3. # def __init__(self, val=0, left=None, right=None):
    4. # self.val = val
    5. # self.left = left
    6. # self.right = right
    7. class Solution:
    8. def isBalanced(self, root: TreeNode) -> bool:
    9. return self.maxDepth(root) != -1
    10. def maxDepth(self, root):
    11. if root == None:
    12. return 0
    13. left = self.maxDepth(root.left)
    14. right = self.maxDepth(root.right)
    15. if left != -1 and right != -1 and abs(left-right) <= 1:
    16. return max(left, right) + 1
    17. else:
    18. return -1