Skip to main content

Minimum Absolute Difference in BST - Solution & Explanation

EasyTreeDepth-First SearchBreadth-First SearchBinary Search Tree19 min readAsked at: Amazon, Meta, Google
Practice this problem

Problem Statement

Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.

 

Example 1:

Input: root = [4,2,6,1,3]
Output: 1

Example 2:

Input: root = [1,0,48,null,null,12,49]
Output: 1

 

Constraints:

  • The number of nodes in the tree is in the range [2, 104].
  • 0 <= Node.val <= 105

 

Note: This question is the same as 783: https://leetcode.com/problems/minimum-distance-between-bst-nodes/

Approach Overview

Problem Overview: You are given the root of a Binary Search Tree and must return the minimum absolute difference between values of any two different nodes. Because the tree follows BST ordering rules, an efficient solution relies on processing node values in sorted order.

Approach 1: In-order Traversal Using Stack (Time: O(n), Space: O(h))

A Binary Search Tree produces values in sorted order when visited using in-order traversal (left → node → right). Instead of recursion, this approach uses an explicit stack to simulate the traversal. As you iterate through nodes, track the value of the previously visited node. The minimum absolute difference must occur between two adjacent values in the sorted sequence, so compute current.val - prev at each step and update the minimum.

The algorithm repeatedly pushes left children onto the stack, pops the next node to process, then moves to the right subtree. Each node is visited exactly once. This makes the time complexity O(n), where n is the number of nodes. The stack stores at most the height of the tree, giving O(h) auxiliary space. Use this version when you want an iterative solution or when recursion depth might be a concern.

Approach 2: Recursive In-order Traversal (Time: O(n), Space: O(h))

This version performs the same sorted traversal but relies on recursion instead of a manual stack. The recursion explores the left subtree, processes the current node, then explores the right subtree. Maintain two variables across calls: the previously visited value and the global minimum difference. Each time the traversal visits a node, compare it with the previous value and update the minimum difference.

The key insight is identical: in a BST, the smallest difference always occurs between two consecutive values in the sorted in-order sequence. Because each node is visited once, the runtime is O(n). The recursion stack grows up to the tree height, so space complexity is O(h). This version is concise and commonly used in interview explanations when discussing Depth-First Search on trees.

Both approaches rely on the ordering property of a tree structured as a BST. Instead of comparing every pair of nodes, which would take O(n²), you only compare neighbors in the sorted traversal order.

Recommended for interviews: The recursive in-order traversal is the most common explanation because it clearly demonstrates how BST ordering reduces the problem to comparing adjacent values. Interviewers often accept either version, but showing the stack-based implementation proves you understand how recursion works under the hood.

Approach 1: In-order Traversal Using Stack

In this approach, we will perform an in-order traversal of the BST using an explicit stack to store the node values in a sorted manner. As we traverse the tree, we will calculate the minimum difference between consecutive values.

The C solution uses an iterative approach with a stack to perform an in-order traversal. The stack mimics the call stack used in recursion, storing nodes to revisit them in the correct order. The minimum difference is updated whenever a valid consecutive pair is found.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(N), where N is the number of nodes. Each node is visited exactly once.
Space Complexity: O(H), where H is the height of the tree, representing the maximum size of the stack.

Try this approach in the editor →

Approach 2: Recursive In-order Traversal

This approach relies on a recursive in-order traversal of the BST to compute the minimum absolute difference. We maintain a global variable to track the smallest difference encountered during traversal.

The C solution implements recursive in-order traversal with helper function inOrder. It retains global variables for previous node value and minimum difference, updating them through recursive traversal.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(N)
Space Complexity: O(H), due to recursive call stack.

Try this approach in the editor →

Approach 3: Inorder Traversal

The problem requires us to find the minimum difference between the values of any two nodes. Since the inorder traversal of a binary search tree is an increasing sequence, we only need to find the minimum difference between the values of two adjacent nodes in the inorder traversal.

We can use a recursive method to implement the inorder traversal. During the process, we use a variable pre to save the value of the previous node. This way, we can calculate the minimum difference between the values of two adjacent nodes during the traversal.

The time complexity is O(n), and the space complexity is O(n). Here, n is the number of nodes in the binary search tree.

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
In-order Traversal Using Stack

Time Complexity: O(N), where N is the number of nodes. Each node is visited exactly once.
Space Complexity: O(H), where H is the height of the tree, representing the maximum size of the stack.

Recursive In-order Traversal

Time Complexity: O(N)
Space Complexity: O(H), due to recursive call stack.

Inorder Traversal—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
In-order Traversal Using StackO(n)O(h)When you prefer an iterative solution or want to avoid recursion depth limits.
Recursive In-order TraversalO(n)O(h)Most common interview explanation; concise and easy to implement for BST problems.

Video Solution

Minimum Absolute Difference in BST - Leetcode 530 - Trees (Python) • Greg Hogg • 11,763 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum Absolute Difference in BST easy or hard?
Minimum Absolute Difference in BST is classified as an Easy problem on LeetCode. The main concept is recognizing that in-order traversal of a BST produces sorted values, which reduces the problem to comparing adjacent elements.
How to solve Minimum Absolute Difference in BST in O(n)?
Perform an in-order traversal so the BST values are processed in sorted order. Maintain a variable storing the previous node's value and compute the difference with the current node. Update the minimum difference at each step. Since every node is visited once, the algorithm runs in O(n) time.
What is the best approach for Minimum Absolute Difference in BST?
The best approach uses in-order traversal of the Binary Search Tree. In-order traversal outputs node values in sorted order, so the minimum absolute difference must occur between two consecutive values. By tracking the previous node value during traversal, you can compute the difference in O(n) time and O(h) space.
What data structure is used in Minimum Absolute Difference in BST?
The problem uses a Binary Search Tree as the core data structure. The solution typically applies depth-first search with in-order traversal, using either recursion or a stack to process nodes while maintaining the previous value and the current minimum difference.
What is the time complexity of Minimum Absolute Difference in BST?
The optimal solution runs in O(n) time because each node in the tree is visited exactly once during in-order traversal. The space complexity is O(h), where h is the height of the tree, due to the recursion stack or the explicit stack used for traversal.
Minimum Absolute Difference in BST Python or Java solution approach?
Python and Java implementations both follow the same idea: perform an in-order traversal and compare each node's value with the previously visited value. Maintain a global or external variable for the minimum difference. This results in O(n) time and O(h) space for both languages.
Is Minimum Absolute Difference in BST asked at Google, Amazon, or Meta?
Binary Search Tree traversal problems like this frequently appear in interviews at companies such as Amazon, Google, and Meta. They test understanding of BST properties, depth-first traversal, and recognizing when sorted order eliminates unnecessary comparisons.

Ready to solve this problem?

Practice Minimum Absolute Difference in BST with our built-in code editor and test cases.

Practice on FleetCode