Data Structures · Visualization

Binary Tree: Is Balanced

Binary Tree

A tree is height-balanced if, for every node, the heights of its left and right subtrees differ by no more than 1. Time: O(n) with the optimized bottom-up approach. Space: O(h) for the recursion stack.

Big-O: Traversals visit every node exactly once: O(n).

Binary Tree: Is Balanced

Binary Tree

A tree is height-balanced if, for every node, the heights of its left and right subtrees differ by no more than 1. This check is done recursively from the bottom up: compute each subtree's height while simultaneously checking the balance condition, stopping early if any imbalance is found.

How Is Balanced Works

  1. If the current node is null, its height is 0 and it is balanced (base case).
  2. Recursively check the left subtree, getting its height and balance status.
  3. Recursively check the right subtree, getting its height and balance status.
  4. The current node is balanced only if both subtrees are balanced AND their heights differ by at most 1.
  5. Return the combined balance status upward.

Is Balanced Worked Example

Input: Tree: root(1) -> left(2) -> left(3); root -> right(4)

  1. Left subtree (rooted at 2) has height 2; right subtree (rooted at 4) has height 1
  2. Difference is 1, so root is balanced so far
  3. Check node 2: left child 3 (height 1), right child null (height 0) → difference 1, balanced

Output: true (the tree is balanced)

Binary Tree: Introduction
A hierarchy of nodes where each node has at most two children — usually called "left" and "right".
45
20
10
30
40
65
50
60
70
80
Active Result
Binary Tree: Introduction
Press ▶ Run to watch it step by step.
Python · Binary Trees in Code
def is_balanced(root):
    def dfs(n):
        if not n:
            return 0
        l = dfs(n.left)
        r = dfs(n.right)
        if l == -1 or r == -1 or abs(l - r) > 1:
            return -1       # unbalanced
        return 1 + max(l, r)
    return dfs(root) != -1
# example: is balanced
Input: Tree: root(1) -> left(2) -> left(3); root -> right(4)

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Is Balanced Time & Space Complexity

TimeO(n) with the optimized bottom-up approach
SpaceO(h) for the recursion stack

When to Use Is Balanced

  • Validating self-balancing tree implementations like AVL trees
  • Deciding when a tree needs rebalancing to keep operations fast
  • A common interview question about combining recursion with early exits

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.