ExamAdda Logo

Minimum Absolute Difference in BST

Easy

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

Important BST Property

An inorder traversal of a BST gives values in sorted order:

Left → Root → Right

 

Therefore, the minimum difference will always be between two adjacent values in the inorder traversal.

Example

       4     

      / \    

    2   6  

   / \   

 1   3

 

Inorder traversal:

1 2 3 4 6

 

Differences:

2 - 1 = 1 3 - 2 = 1 4 - 3 = 1 6 - 4 = 2

 

Answer:

1

Example 1

Input

root = [4,2,6,1,3]

Output

1

Explanation

The BST is:

      4      

     / \     

   2   6  

   / \  

  1   3

 

Inorder traversal produces the sorted sequence:

1 2 3 4 6

 

Now compare adjacent values:

2 - 1 = 1 3 - 2 = 1 4 - 3 = 1 6 - 4 = 2

 

The minimum absolute difference is:

1

Example 2

Input

root = [1,0,48,null,null,12,49]

Output

1

Explanation

Inorder traversal gives:

0 1 12 48 49

 

Adjacent differences are:

1 - 0  = 1 12 - 1 = 11 48 - 12 = 36 49 - 48 = 1

 

Therefore, the minimum difference is:

1

 

Constraints

2 <= number of nodes <= 10^4
0 <= Node.val <= 10^5
All Node.val are unique.

Hints:

Hint 1

Remember that inorder traversal of a BST produces values in sorted order.

Hint 2

You only need to compare adjacent values in the sorted sequence.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 13, 2026

Last updated

Aug 14, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output