How to make this faster?


  • 0
    U
    class Solution(object):
        def isBalanced(self, root):
            """
            :type root: TreeNode
            :rtype: bool
            """
            if not root:
                return True
                
            left = self.depth(root.left)
            right = self.depth(root.right)
            
            return abs(left - right) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right)
        
        def depth(self, root):
            if root == None:
                return 0
            return max(self.depth(root.left), self.depth(root.right))+1
    

Log in to reply
 

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