Textbook solution beats 91% of Python submissions


  • 2
    J
    def isBalanced(self, root):
        if self.checkHeight(root) == -1:  
            return False
        else:  
            return True
    
    def checkHeight(self, root):
        if root == None:  
            return 0
        leftHeight = self.checkHeight(root.left)
        if leftHeight == -1:
            return -1
        rightHeight = self.checkHeight(root.right)
        if rightHeight == -1:
            return -1
        diffHeight = abs(leftHeight - rightHeight)
        if diffHeight > 1:
            return -1
        else:
            return max(leftHeight, rightHeight) + 1

Log in to reply
 

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