THN Interview Prep

110. Balanced Binary Tree

At a Glance

  • Topic: trees
  • Pattern: Tree DFS (post-order)
  • Difficulty: Easy
  • Companies: Amazon, Google, Bloomberg, Meta, Apple
  • Frequency: High
  • LeetCode: 110

Problem (one-liner)

Given the root of a binary tree, determine if it is height-balanced: for every node, the absolute difference between the heights of its left and right subtrees is at most 1.

Recognition Cues

  • "Balanced" binary tree definition (AVL-style height gap)
  • Check every subtree, not only the root’s children
  • Often solved by returning sentinel height -1 when imbalanced

Diagram

At-a-glance flow (replace with problem-specific Mermaid as you refine this note). camelCase node IDs; no spaces in IDs.

Loading diagram…

Approaches

  • Brute forceO(n²) time — for each node, compute subtree heights with separate DFS each time.
  • BetterO(n) time — cache heights in a map (two passes).
  • OptimalO(n) time / O(h) stack — single post-order DFS: return -1 if child imbalanced or |left−right| > 1; else return 1 + max(left, right).

Optimal Solution

Go

package main

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func isBalanced(root *TreeNode) bool {
	var height func(*TreeNode) int
	height = func(node *TreeNode) int {
		if node == nil {
			return 0
		}
		leftHeight := height(node.Left)
		if leftHeight == -1 {
			return -1
		}
		rightHeight := height(node.Right)
		if rightHeight == -1 {
			return -1
		}
		diff := leftHeight - rightHeight
		if diff < 0 {
			diff = -diff
		}
		if diff > 1 {
			return -1
		}
		if leftHeight > rightHeight {
			return 1 + leftHeight
		}
		return 1 + rightHeight
	}
	return height(root) != -1
}

JavaScript

class TreeNode {
	constructor(val = 0, left = null, right = null) {
		this.val = val;
		this.left = left;
		this.right = right;
	}
}

function isBalanced(root) {
	function height(node) {
		if (node === null) {
			return 0;
		}
		const leftHeight = height(node.left);
		if (leftHeight === -1) {
			return -1;
		}
		const rightHeight = height(node.right);
		if (rightHeight === -1) {
			return -1;
		}
		if (Math.abs(leftHeight - rightHeight) > 1) {
			return -1;
		}
		return 1 + Math.max(leftHeight, rightHeight);
	}
	return height(root) !== -1;
}

Walkthrough

Tree: 3 / 9,20 / null,null,15,7 — balanced.

nodeleftrightok?return height
1500yes1
700yes1
2011yes2
900yes1
312yes3

If we hang a long chain under 9, height diff at 3 can exceed 1 → return -1 bubbles up.

Invariant: any subtree returns -1 iff it contains an imbalance anywhere inside.

Edge Cases

  • Empty tree → balanced (true)
  • Two nodes → balanced
  • Left-skewed chain → not balanced except short chains

Pitfalls

  • Checking only root’s left/right heights (misses imbalance deeper)
  • Using unsigned math for diff (use abs on signed heights)

Similar Problems

Variants

  • Return first violating node.
  • k-balanced (diff ≤ k) generalization.

Mind-Map Tags

#trees #post-order #height #sentinel #bubble-up-failure

Last updated on

Spotted something unclear or wrong on this page?

On this page