AlgoMaster Logo

Minimum Absolute Difference in BST

Last Updated: March 29, 2026

easy
4 min read

Understanding the Problem

We need to find the smallest absolute difference between any two node values in a BST. At first glance, this might seem like we need to compare every pair of nodes, but the BST property gives us a huge shortcut.

In a BST, the inorder traversal visits nodes in sorted (ascending) order. And here is the critical observation: in a sorted sequence, the minimum absolute difference always occurs between two adjacent elements. You would never find a smaller gap by comparing elements that are further apart in the sorted order. So instead of comparing all pairs, we just need to compare consecutive nodes in the inorder traversal.

Key Constraints:

  • 2 <= number of nodes <= 10^4 → The tree always has at least 2 nodes, so there is always a valid pair to compare. With up to 10,000 nodes, O(n) solutions are comfortable and even O(n log n) is fine.
  • 0 <= Node.val <= 10^5 → Values are non-negative integers, so no overflow concerns. The maximum possible difference is 10^5, which fits easily in an int.

Approach 1: Inorder Traversal with Array

Intuition

The most natural approach for a beginner: collect all node values via inorder traversal, then scan through the resulting sorted list to find the minimum gap between consecutive elements.

Why inorder? Because in a BST, an inorder traversal (left, node, right) visits nodes in ascending order. That gives us a sorted array without any sorting step. And once we have a sorted array, finding the minimum absolute difference is trivial: just compare each element with its neighbor.

Algorithm

  1. Perform an inorder traversal of the BST and collect all node values into a list.
  2. Iterate through the list from index 1 to the end.
  3. At each index, compute the difference between the current element and the previous one.
  4. Track the minimum difference seen so far.
  5. Return the minimum difference.

Example Walkthrough

1Start inorder traversal: go left from root (4)
421curr36
1/6
1Values array empty, starting traversal
1/6

Code

This approach works but stores every value when we only ever compare adjacent pairs. What if we tracked just the previous node during traversal and computed differences on the fly?

Approach 2: Inorder Traversal with Previous Pointer (Optimal)

Intuition

Instead of collecting all values into a list and then scanning for the minimum gap, we can compute the gap as we go. During the inorder traversal, we keep a reference to the previously visited node. Each time we visit a new node, we compute the difference between it and the previous node, and update our running minimum.

This is the same logic as Approach 1, just without the intermediate list. We are streaming through the sorted sequence and computing the answer in real time.

Algorithm

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

Example Walkthrough

1Start inorder: visit node 1, prev=null, skip comparison
421curr36
1/6

Code

The time is optimal, but the recursion stack uses O(h) space. Can we eliminate the stack entirely using Morris traversal for true O(1) space?

Approach 3: Morris Inorder Traversal (Space Optimal)

Intuition

Morris traversal is a clever technique that lets us do an inorder traversal without any extra space: no recursion stack, no explicit stack, nothing. It temporarily modifies the tree structure by creating "threaded" links from each node's inorder predecessor back to the node itself, which lets us return to parent nodes without a stack.

The core idea: before going left, find the rightmost node in the left subtree (the inorder predecessor). Make a temporary link from that node back to the current node. When we later follow this link back, we know we have finished the left subtree and can process the current node.

Algorithm

  1. Initialize current to the root, prev to null, and minDiff to infinity.
  2. While current is not null:

a. If current has no left child, process current (compare with prev, update minDiff), set prev = current, then move to current.right.

b. If current has a left child, find the inorder predecessor (rightmost node in the left subtree).

  • If the predecessor's right child is null, set it to current (create thread), then move current to current.left.
  • If the predecessor's right child is already current (thread exists), remove the thread, process current, set prev = current, then move to current.right.
  1. Return minDiff.

Example Walkthrough

1current=4, has left. Find predecessor (3), create thread 3->4, go left
4curr213pred6
1/7

Code