AlgoMaster Logo

Minimum Distance Between BST Nodes

easyFrequency6 min readUpdated June 23, 2026

Understanding the Problem

We need to find the smallest absolute difference between the values of any two distinct nodes in a BST. Checking every pair of nodes works, but the BST property leads to a faster approach.

An inorder traversal of a BST visits nodes in ascending sorted order. In a sorted sequence, the minimum difference between any two elements always occurs between consecutive elements. Take the sorted values [1, 3, 6, 10]: the smallest gap is between 1 and 3 (a difference of 2), and no non-consecutive pair like 1 and 10 can produce a smaller gap. 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 small, so even an O(n^2) brute force over all pairs would finish quickly. The interesting solution exploits the BST inorder property.
  • 0 <= Node.val <= 10^5 → Values are non-negative integers, and the maximum gap fits in a 32-bit int, so subtraction never overflows.
  • The tree is a valid BST, so inorder traversal produces a sorted sequence.

Approach 1: Inorder Traversal with Sorting

Intuition

Collect all the values from the BST, sort them, then scan for the minimum gap between consecutive elements. This treats the BST like any other binary tree: the traversal order does not matter because the explicit sort puts the values in order regardless.

This is a useful baseline because it works on any binary tree, not only a BST. The later approaches improve on it by using the BST property to avoid the sort.

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 uses 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

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

Code