Skip to content

Minimum Absolute Difference in BST

EasyTrees
Decode it first. Clues, constraints, pattern and plan before any code.

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.

[8, 4, 13, 2, 6]                  →  2   (4 and 6, or 4 and 2, or 6 and 8)
[10, 3, 40, null, null, 21, 45]   →  5   (40 and 45)
[1, null, 100000]                 →  99999

Constraints

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