Minimum Distance Between BST Nodes

Easy

Description

Given the root of a Binary Search Tree, return the minimum 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,3,7,null,18]
Output:2
Explanation:

The minimum difference is 2, which occurs between nodes 5 and 7. Other differences include: 7-5=2, 10-7=3, 15-10=5, etc.

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

This right-skewed tree has minimum difference of 7 between nodes 27 and 34. The inorder traversal gives [27,34,50,58] with differences [7,16,8].

Constraints

  • 2 ≤ nodes ≤ 100

Ready to solve this problem?

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