https://leetcode.com/problems/balanced-binary-tree

What we need to do here is just to traverse our tree, using for example dfs and check balance for every node.

dfs(node) here returns depth of subtree with root in node. If it is None, depths is equal to 0. We evaluate depths of left and right subtee and return maximum of them plus one. Also we check balance and if absolute difference is more than 1, we can mark variable self.Bal as False: we can state now that tree is not balanced.

Complexity: time complexity is O(n), space complexity is O(h) as any classical dfs have.

class Solution:
    def isBalanced(self, root):
        self.Bal = True
        
        def dfs(node):
            if not node: return 0
            lft, rgh = dfs(node.left), dfs(node.right)
            if abs(lft - rgh) > 1: self.Bal = False
            return max(lft, rgh) + 1
            
        dfs(root)
        return self.Bal

If you like the solution, you can upvote it on leetcode discussion section: Problem 0110