We need to find the smallest absolute difference between the values of any two distinct nodes in a BST. Checking every pair of nodes works, but the BST property leads to a faster approach.
An inorder traversal of a BST visits nodes in ascending sorted order. In a sorted sequence, the minimum difference between any two elements always occurs between consecutive elements. Take the sorted values [1, 3, 6, 10]: the smallest gap is between 1 and 3 (a difference of 2), and no non-consecutive pair like 1 and 10 can produce a smaller gap. The problem reduces to finding the minimum difference between consecutive values in the inorder sequence.
2 <= number of nodes <= 100 → The tree is small, so even an O(n^2) brute force over all pairs would finish quickly. The interesting solution exploits the BST inorder property.0 <= Node.val <= 10^5 → Values are non-negative integers, and the maximum gap fits in a 32-bit int, so subtraction never overflows.Collect all the values from the BST, sort them, then scan for the minimum gap between consecutive elements. This treats the BST like any other binary tree: the traversal order does not matter because the explicit sort puts the values in order regardless.
This is a useful baseline because it works on any binary tree, not only a BST. The later approaches improve on it by using the BST property to avoid the sort.
Input:
After collecting and sorting: values = [1, 2, 3, 4, 6]
Scan consecutive pairs: (1,2)=1, (2,3)=1, (3,4)=1, (4,6)=2. Minimum difference = 1.
We are sorting the collected values at O(n log n), but a BST already has its values in sorted order. What if we traversed the BST in order and collected values that are already sorted?
Since a BST's inorder traversal naturally produces values in sorted order, we can skip the explicit sort. Just do an inorder traversal (left, root, right), collect the values, and then scan consecutive pairs for the minimum difference.
This uses the BST property directly. The values come out sorted, so we just need one pass through the resulting list to find the smallest gap.
The BST property guarantees that inorder traversal produces a sorted sequence. And in any sorted sequence, the minimum absolute difference always occurs between adjacent elements. If you have three sorted values a < b < c, then (c - a) = (c - b) + (b - a). Both (c - b) and (b - a) are positive, so (c - a) is always larger than either of them. This means you never need to compare non-adjacent elements.
We are storing all n values in a list just to compare consecutive pairs. What if we tracked just the previous value during the traversal and computed differences on the fly?
Instead of collecting all values and comparing afterwards, we can compute the minimum difference during the inorder traversal itself. We just need to remember the previously visited node's value. Each time we visit a new node in inorder sequence, we compare it with the previous value, update the minimum difference, and then move the previous pointer forward.
This is the cleanest solution. One pass, constant extra space (ignoring the recursion stack), and it directly exploits the BST property.
prev to null (or -1) and minDiff to infinity.prev is set, compute current.val - prev and update minDiff if this difference is smaller.prev to current.val before moving to the next node.minDiff.