68ms beats 98.65% very simple python solution with recursion


  • 0
    D
    class Solution(object):
        def isBalanced(self, root):
            """
            :type root: TreeNode
            :rtype: bool
            """
            if not root: return True
            return bool(self.depthBalance(root, 0))
            
        def depthBalance(self, node, depth):
            if not node: return depth
            l = self.depthBalance(node.left, depth+1)
            r = self.depthBalance(node.right, depth+1)
            if not l or not r or abs(l-r) > 1:
                return False
            else:
                return max(l, r) 
    

Log in to reply
 

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