Naive python solution beating 98%, slightly change MaxDepth


  • 2
    C
    def isBalanced(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if not root:
            return True
        if self.depth(root) == -1:
            return False
        else:
            return True
            
    def depth(self,root):
        
        if not root:
            return 0
        
        left = self.depth(root.left)
        right = self.depth(root.right)
        
        if abs(left-right)>1 or left == -1 or \
        right == -1:
            return -1
        return max(left,right)+1

Log in to reply
 

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