Balanced Binary Tree

Algorithms
Easy
Spotify
42.9K views

Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is one in which the depth of the two subtrees of every node never differs by more than one.

Why Interviewers Ask This

Spotify asks this to assess your ability to think recursively and optimize for time complexity beyond basic traversal. They want to see if you recognize that a naive solution checking height at every node leads to O(N^2) inefficiency, whereas an optimal approach computes height and balance status in a single pass.

How to Answer This Question

1. Clarify the definition: Explicitly state that a tree is balanced if the height difference between left and right subtrees of every node is at most one. 2. Propose the naive approach first: Briefly explain calculating height separately for each node, noting its O(N^2) complexity to show awareness of inefficiency. 3. Pivot to the optimal strategy: Describe a bottom-up recursive approach where you return both the height and a boolean flag from each call. 4. Define the base case: Explain that an empty node returns height zero and true (balanced). 5. Implement the logic: Detail how to check if children are balanced and if their height difference exceeds one; if so, propagate false immediately to prune the search space. 6. Analyze complexity: Conclude by stating the final solution runs in O(N) time with O(H) space due to recursion stack, demonstrating efficiency suitable for Spotify's high-scale data needs.

Key Points to Cover

  • Recognizing that a naive solution is O(N^2) and explaining why it fails
  • Implementing a bottom-up recursive strategy that combines height calculation and balance checking
  • Understanding how to prune the recursion early when an imbalance is detected
  • Correctly defining base cases for null nodes returning height zero
  • Explicitly stating the final Time and Space complexity analysis

Sample Answer

To determine if a binary tree is height-balanced, we need to ensure that for every node, the depth of its left and right subtrees differs by no more than one. A common initial thought is to write a helper function to calculate the height of each subtree independently and then compare them at every node. However, this approach recalculates heights repeatedly, resulting in a time complexity of O(N^2), which is inefficient for large datasets typical at Spotify. A much better approach is to use a single recursive pass that returns two pieces of information: the height of the current subtree and whether it is balanced. We start at the leaves. If a node is null, we return a height of zero and a balanced status of true. For any other node, we recursively call our function on both the left and right children. Inside the recursion, we first check if either child returned an unbalanced status. If so, the current node is also unbalanced, and we can immediately return false without further calculation. Next, we compare the absolute difference between the left and right heights. If this difference is greater than one, the tree is unbalanced. Otherwise, we return the maximum height of the two children plus one, along with true. This ensures we visit each node exactly once, achieving O(N) time complexity and O(H) space complexity for the recursion stack, which is the optimal solution expected in technical interviews.

Common Mistakes to Avoid

  • Calculating height separately for every node, leading to unnecessary O(N^2) performance issues
  • Forgetting to return the boolean status alongside the height value in the recursive calls
  • Only checking the root node's balance instead of verifying every single node in the tree
  • Not handling the edge case where the height difference is exactly one versus two correctly

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 30 Spotify questions