Minimum Absolute Difference in BST

EasyTreesbst

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given the root of a binary search tree with at least two nodes and unique non-negative values, return the smallest absolute difference between the values of any two different nodes. The two nodes do not need to be adjacent in the tree. Aim for O(n) time.

Examples

Input: root = [4,2,6,1,3]
Output: 1
Several pairs differ by 1, for example 1 and 2.
Input: root = [1,0,48,null,null,12,49]
Output: 1
0 and 1 (or 48 and 49) differ by 1.

Constraints

  • 2 <= number of nodes <= 10^4
  • 0 <= Node.val <= 10^5
  • All values are unique and form a valid BST.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More bst problems