#530

Minimum Absolute Difference in BST

Easy Inorder = sorted

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the smallest difference between the values of any two nodes in a BST.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the smallest difference between any two values.

Why this works: inorder gives sorted values; the closest pair is always neighbours in sorted order.

Example: 4 (2 (1, 3), 6)

inorder 1, 2, 3, 4, 6 → gaps 1, 1, 1, 2 → 1

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Only comparing a node with its direct children — the closest value can be far away in the tree.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(h).
Topic: Binary Search Trees
🎬 Episodes 61–63: video coming soon