You're given the root of a Binary Search Tree (BST) and need to find the smallest possible absolute difference between the values of any two different nodes in the entire tree.
Think of it as finding the two nodes whose values are closest together when you ignore the sign of their difference. For example, if you have nodes with values 2 and 5, the absolute difference is |2-5| = 3.
Goal: Return the minimum absolute difference as an integer.
Input: The root node of a BST
Output: An integer representing the minimum absolute difference
Key Insight: Since this is a BST, an in-order traversal will give you all values in sorted order, making it easier to find consecutive differences!
Input & Output
Visualization
Time & Space Complexity
We visit each node exactly once during the in-order traversal
Only recursion stack space needed, where h is the height of the tree (O(log n) for balanced BST, O(n) for skewed tree)
Constraints
- The number of nodes in the tree is in the range [2, 104]
- 0 โค Node.val โค 105
- The tree is guaranteed to be a valid BST