AlgoMaster Logo

Minimum Distance Between BST Nodes

Last Updated: March 29, 2026

easy
4 min read

Understanding the Problem

We need to find the smallest absolute difference between the values of any two distinct nodes in a BST. At first glance, you might think about checking every pair of nodes, but the BST property gives us a much better approach.

Here is the key insight: in a BST, an inorder traversal visits nodes in ascending sorted order. When values are sorted, the minimum difference between any two elements must occur between consecutive elements. Think about it. If you have sorted values [1, 3, 6, 10], the minimum gap is between 1 and 3 (difference of 2). You would never find a smaller gap between non-consecutive elements like 1 and 10. So the problem reduces to finding the minimum difference between consecutive values in the inorder sequence.

Key Constraints:

  • 2 <= number of nodes <= 100 → The tree is very small. Even an O(n^2) brute force checking all pairs would handle 100 nodes in microseconds. But the problem is really testing whether you understand the BST inorder property.
  • 0 <= Node.val <= 10^5 → Node values are non-negative integers. No overflow concerns with simple subtraction.
  • The tree is guaranteed to be a valid BST, so inorder traversal will produce a sorted sequence. This is the property we should exploit.

Approach 1: Inorder Traversal with Sorting

Intuition

The most natural first approach is to collect all the values from the BST, sort them, and then scan for the minimum gap between consecutive elements. Since it is a BST, inorder traversal already gives us sorted order, but even if you forget that property and just collect values in any order, sorting them afterwards still works.

This approach treats the BST like any other tree. Gather all values, sort them, and compare neighbors. Simple and correct.

Algorithm

  1. Traverse the entire BST (any traversal order works) and collect all node values into a list.
  2. Sort the list in ascending order.
  3. Iterate through the sorted list and compute the difference between each pair of consecutive elements.
  4. Return the minimum difference found.

Example Walkthrough

Input:

42136
root

After collecting and sorting: values = [1, 2, 3, 4, 6]

0
1
1
2
2
3
3
4
4
6
values

Scan consecutive pairs: (1,2)=1, (2,3)=1, (3,4)=1, (4,6)=2. Minimum difference = 1.

Code

We are sorting the collected values at O(n log n), but a BST already has its values in sorted order. What if we traversed the BST in order and collected values that are already sorted?

Approach 2: Inorder Traversal into List

Intuition

Since a BST's inorder traversal naturally produces values in sorted order, we can skip the explicit sort. Just do an inorder traversal (left, root, right), collect the values, and then scan consecutive pairs for the minimum difference.

This leverages the BST property directly. The values come out sorted, so we just need one pass through the resulting list to find the smallest gap.

Algorithm

  1. Perform an inorder traversal of the BST to collect all values into a list (they will be in ascending order).
  2. Iterate through the list from index 1 onward, computing the difference between each element and its predecessor.
  3. Track and return the minimum difference.

Example Walkthrough

1Start inorder traversal: go to leftmost node (1)
421visit36
1/6
1Collect 1 into values list
0
1
1/6

Code

We are storing all n values in a list just to compare consecutive pairs. What if we tracked just the previous value during the traversal and computed differences on the fly?

Approach 3: Inorder Traversal with Previous Pointer (Optimal)

Intuition

Instead of collecting all values and comparing afterwards, we can compute the minimum difference during the inorder traversal itself. We just need to remember the previously visited node's value. Each time we visit a new node in inorder sequence, we compare it with the previous value, update the minimum difference, and then move the previous pointer forward.

This is the cleanest solution. One pass, constant extra space (ignoring the recursion stack), and it directly exploits the BST property.

Algorithm

  1. Initialize prev to null (or -1) and minDiff to infinity.
  2. Perform an inorder traversal of the BST.
  3. At each node, if prev is set, compute current.val - prev and update minDiff if this difference is smaller.
  4. Set prev to current.val before moving to the next node.
  5. After the traversal completes, return minDiff.

Example Walkthrough

1Visit 0 (leftmost), prev=null, no comparison. Set prev=0
10visit481249
1/6
1No comparison yet (first node)
Infinity
1/6

Code