Skip to content
Decoder

Minimum Absolute Difference in BST

Easy

Problem

Given the root of a binary search tree (every node's left subtree holds smaller values and its right subtree holds larger values), return the smallest difference between the values of any two different nodes.

Trees in the examples are written in level order (top to bottom, left to right), with null for a missing child, the same way the tests build them.

Example 1

Input
[8, 4, 13, 2, 6]
Output
2
Why
4 and 6, or 4 and 2, or 6 and 8

Example 2

Input
[10, 3, 40, null, null, 21, 45]
Output
5
Why
40 and 45

Example 3

Input
[1, null, 100000]
Output
99999

Constraints

  • 2 <= number of nodes <= 10^4
  • 0 <= Node.val <= 10^5
  • All values are distinct.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.