Easy Python Solution, 6 lines

  • 1
    class Solution(object):
        def isBalanced(self, root):
            return self.helper(root, 0)[0]
        def helper(self, root, depth):
            if root:
                a_l, a_r = self.helper(root.left, depth+1), self.helper(root.right, depth+1)
                return (-1 <= a_l[1] - a_r[1] <= 1, max(a_l[1], a_r[1])) if a_l[0] and a_r[0] else (False, 0)
                return (True, depth)

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.