#530 Minimum Absolute Difference in BST
the problem
In plain words
Given the root of a BST with at least two nodes, return the smallest difference between the values of any two different nodes.
in root = [4,2,6,1,3]
out 1
Sorted values 1, 2, 3, 4, 6: neighbours differ by 1, 1, 1, 2.
in root = [1,0,48,null,null,12,49]
out 1
48 and 49.
- 2 ≤ number of nodes ≤ 10⁴
- 0 ≤ Node.val ≤ 10⁵
- Same problem as LC 783 (Minimum Distance Between BST Nodes)
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternBST property
In a sorted list, the closest pair always sits side by side. Inorder gives you the BST's values sorted — so compare each value only with the one just before it.
solutions
From brute force to optimal
Ignore the order: collect all values and compare each pair.
- Collect values in any traversal.
- For every pair i < j, best = min(best, |a[i] − a[j]|).
n(n − 1)/2 pairs — about 5·10⁷ for n = 10⁴.
The list of values.
class Solution { /** Ignores the BST order: compare every pair of values. */ public int getMinimumDifference(TreeNode root) { List<Integer> values = new ArrayList<>(); collect(root, values); int best = Integer.MAX_VALUE; for (int i = 0; i < values.size(); i++) for (int j = i + 1; j < values.size(); j++) best = Math.min(best, Math.abs(values.get(i) - values.get(j))); return best; } private void collect(TreeNode node, List<Integer> out) { if (node == null) return; out.add(node.val); collect(node.left, out); collect(node.right, out); }}Inorder gives sorted values; check adjacent differences.
- Inorder into a list.
- best = min over i of list[i] − list[i − 1].
One traversal plus one scan.
The list.
class Solution { /** Inorder gives the values sorted, and the closest pair in a sorted list sits side by side. */ public int getMinimumDifference(TreeNode root) { List<Integer> sorted = new ArrayList<>(); inorder(root, sorted); int best = Integer.MAX_VALUE; for (int i = 1; i < sorted.size(); i++) best = Math.min(best, sorted.get(i) - sorted.get(i - 1)); return best; } private void inorder(TreeNode node, List<Integer> out) { if (node == null) return; inorder(node.left, out); out.add(node.val); inorder(node.right, out); }}Same comparison, but remember only the previous value while walking.
- prev = null, best = MAX.
- Inorder: at each node, if prev != null, best = min(best, node.val − prev).
- prev = node.val.
Each node visited once.
Only the recursion stack; no list.
class Solution { private int best; private Integer prev; // the value visited just before, in sorted order (null at the start) public int getMinimumDifference(TreeNode root) { best = Integer.MAX_VALUE; prev = null; inorder(root); return best; } private void inorder(TreeNode node) { if (node == null) return; inorder(node.left); if (prev != null) best = Math.min(best, node.val - prev); // sorted, so node.val > prev prev = node.val; inorder(node.right); }}in the room
How to say it in the interview
talk track
- Start from "inorder of a BST is sorted" — then it's the classic "closest pair in a sorted array".
- Mention why
node.val − prevneeds noMath.abs: inorder guarantees node.val > prev.
traps
- Comparing each node only with its children — the closest pair can be a node and a deep descendant (the root and the rightmost node of its left subtree).
- Keeping
prevandbestas fields without resetting them at the start of each call.
what they ask next
Follow-ups
Two Sum IV - Input is a BST (LC 653)?
Same sorted view: run an ascending and a descending BST iterator as two pointers — O(n) time, O(h) space.
before you submit
Edge cases to test
- Exactly two nodes → their difference.
- The closest pair is a node and a far descendant, not a parent and child.
- The same Solution object called twice → reset prev and best inside the method.
practise next
Similar problems
- #98Validate Binary Search TreeMediumin BST
- #230Kth Smallest Element in a BSTMediumin BST
- #94Binary Tree Inorder TraversalEasyin Traversals