Medium · Trees

Binary Search Tree Iterator

Given the root of a binary search tree, implement BSTIterator with next(), which returns the next smallest value, and hasNext(), which reports whether any value remains; the tests call next() while hasNext() is true and return the values produced, in order.

Examples

Example 1

{
  "tree": [7, 3, 15, null, null, 9, 20]
}

Output: [3, 7, 9, 15, 20]

Example 2

{
  "tree": [4, 2, 6, 1, 3, 5, 7]
}

Output: [1, 2, 3, 4, 5, 6, 7]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Trees problems