dsa · easy
Closest Values in a BST
You are given a binary search tree. Each node is [val, left, right] or None. Return the minimum absolute difference between the values of any two distinct nodes.
Arguments
root— BST as nested [val, left, right] or None
The tree has at least two nodes.
Example
root = [4,[2,[1,None,None],[3,None,None]],[6,None,None]]
The values are 1, 2, 3, 4, 6. Neighbouring values in sorted order differ by 1, 1, 1, and 2. The minimum is 1.
[1,None,[5,[3,None,None],None]] has values 1, 3, 5 → 2.
Constraints
2 <= number of nodes <= 10^4 0 <= node value <= 10^5 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [4,[2,[1,None,None],[3,None,None]],[6,None,None]] Expected: 1
Example 2
Input: [1,None,[5,[3,None,None],None]] Expected: 2