Trains the technique from
LeetCode 530Minimum Absolute Difference in BSTThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A monitoring rig indexes its pressure readings in a binary search tree: every reading in a node's left subtree is smaller than that node's reading, and every reading in its right subtree is larger. All readings in the index are different.
The index arrives as tree, a level-order listing of the nodes. tree[0] is the root's reading. The remaining entries are consumed in order, two at a time, giving the left child and then the right child of each node already listed, taken in the order the nodes were listed. A null entry means there is no child there, and an absent node contributes no entries of its own. The listing stops once every remaining child would be absent.
Return the smallest absolute difference between the readings of two different nodes.
Walk the tree itself rather than sorting the entries of the listing.
Example 1
The index holds the readings 5, 8, 10, 12 and 20. The pair 8 and 10 differ by 2, and no pair of readings in the index is closer than that.
Example 2
The readings are 12, 25, 37, 50, 62, 75 and 87. The pair 25 and 37 differ by 12, which is the closest any two of them come.
Example 3
The index has only the two readings 0 and 100000, so the answer is the difference between them.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def smallest_reading_gap(tree: list[int | None]) -> int:public int smallestReadingGap(Integer[] tree)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.