Minimum Absolute Difference in BST

Easy

Description

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

Examples

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

Min diff between 2 and 3.

Input:root = [10,5,15,2,7,null,20]
Output:2
Explanation:

The BST contains nodes [2,5,7,10,15,20]. The minimum difference is between consecutive nodes 5 and 7, which gives us |7-5| = 2.

Input:root = [27,null,34,null,58,50,null]
Output:7
Explanation:

The BST contains nodes [27,34,50,58]. Checking all differences: |34-27| = 7, |50-34| = 16, |58-50| = 8. The minimum difference is 7 between nodes 27 and 34.

Constraints

  • 2 ≤ nodes ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!