My Simple and Clean Python solution


  • 1
    N
    class Solution(object):
        def isBalanced(self, root):
            """
            :type root: TreeNode
            :rtype: bool
            """
            try:
                self.getDepth(root)
            except:
                return False
            return True
            
        def getDepth(self, node):
            if node == None:
                return 0
            else:
                left = self.getDepth(node.left)
                right = self.getDepth(node.right)
                if abs(left - right) > 1:
                    raise Exception
                return 1 + max(left, right)
    

Log in to reply
 

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