Minimum Absolute Difference in BST

Given the root of a binary search tree, return the minimum absolute difference between the values of any two different nodes. Pattern focus: Inorder Gives Sorted Sequence. Because inorder traversal of a BST yields a sorted sequence, the minimum difference will be between adjacent values in that order.

Input Format

root = binary tree root

Output Format

minimum absolute difference between any two node values

Constraints

  • 2 <= number of nodes <= 10^5
  • -10^9 <= node values <= 10^9
  • root must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

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

Output:

1

Explanation:

Adjacent inorder values 1 and 2, or 2 and 3, differ by 1.

Example 2:

Input:

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

Output:

1

Explanation:

The closest values in inorder order differ by 1.

Loading...
Minimum Absolute Difference in BST