Intuitive recursive solution


  • 0
    E
    class Solution(object):
        def isBalanced(self, root):
            """
            :type root: TreeNode
            :rtype: bool
            """
            if root is None:
                return True
            else:
                return self._is_node_balanced(root) and self.isBalanced(root.left) and self.isBalanced(root.right)
            
        def _is_node_balanced(self, node):
            return abs(self._depth(node.left) - self._depth(node.right)) <= 1
            
        def _depth(self, node):
            if node is None:
                return 0
            else:
                return 1 + max(self._depth(node.left), self._depth(node.right))
    

Log in to reply
 

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