KatInterview

Height-Balanced Nodes in Heap-Array Trees

Trees & GraphsModel answer
MediumModel answerTrees & Graphsbinary-treepost-orderheap-layout

Last updated

Traverse a binary tree stored in heap-index layout to find every node whose subtree is height-balanced, returning each with its tree level.

Balanced Subtrees in an Array-Backed Binary Tree

A binary tree is stored as a zero-indexed array nodes using the standard heap layout:

  • Index 0 holds the root.
  • A node at index i has its left child at 2*i + 1 and its right child at 2*i + 2.
  • A None entry means no node exists at that position. Any index >= len(nodes) is also treated as empty.
  • A node is only considered part of the tree if every ancestor up to the root is non-None. A non-None entry whose parent is None is ignored.

Height definition:

  • An empty subtree has height 0.
  • A non-empty subtree has height 1 + max(left_height, right_height).

Height-balanced subtree: A subtree rooted at node u is height-balanced if:

  1. Its left subtree is height-balanced (empty counts as balanced).
  2. Its right subtree is height-balanced.
  3. The absolute difference between left and right subtree heights is at most 1.

Your task: implement balanced_nodes(nodes) that returns all present nodes whose subtree is height-balanced. For each, report (index, level), where level is 0-indexed from the root. Sort results by level ascending, then by index ascending.

def balanced_nodes(nodes: list[int | None]) -> list[tuple[int, int]]:
    pass

Constraints

  • 0 <= len(nodes) <= 10^5
  • nodes[i] is an integer in [-10^9, 10^9] or None.

Examples

Example 1

Input:  nodes = [1, 2, None, 3]
Output: [(1, 1), (3, 2)]

Node 0 has a left subtree of height 2 and a right subtree of height 0; difference is 2, so it is not balanced. Node 1 has heights 1 (left) and 0 (right); difference is 1, both children balanced — node 1 is balanced at level 1. Node 3 is a leaf — trivially balanced at level 2.

Example 2

Input:  nodes = [1, 2, 3, 4, None, 6, 7]
Output: [(0, 0), (1, 1), (2, 1), (3, 2), (5, 2), (6, 2)]

Every node in this tree is balanced. The root's left subtree (height 2) and right subtree (height 2) differ by 0.

Example 3

Input:  nodes = []
Output: []

Hints

Hint 1

Height and balance together require information from children before a parent can be evaluated — think about what traversal order naturally satisfies that, and whether the heap index layout already encodes it without any explicit recursion.

Hint 2

A node's level in a heap-indexed tree can be read directly from its index: level = (i + 1).bit_length() - 1. Sketch out a few indices to convince yourself, then think about whether collecting results in ascending index order already gives you the required sort for free.

Hint 3

Watch out for 'orphaned' entries: a non-None value at index i is irrelevant if any ancestor is None. A single forward pass over indices 0..n-1 can mark which nodes are truly present before you compute anything else.