InterviewHubPRO
Section Overview
Q56

Question 56 of 100

Part 7: Binary Trees, BSTs & Traversals (53 - 64)

56%
DSA Patterns (Multi-Lang)•Part 7: Binary Trees, BSTs & Traversals (53 - 64)

Balanced Binary Tree.

In Simple Words: Bottom-up check: return height if balanced, or -1 immediately if any subtree has |left - right| > 1. Avoids redundant $O(N^2)$ checks.

  • Time: $O(N)$ | Space: $O(H)$
Multi-Language Solutions
Switch tabs or choose All
def isBalanced(root: TreeNode | None) -> bool:
    def check(node):
        if not node: return 0
        left = check(node.left)
        if left == -1: return -1
        right = check(node.right)
        if right == -1: return -1
        if abs(left - right) > 1: return -1
        return 1 + max(left, right)
    return check(root) != -1