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

Open every published interview problem, solution, scene, and memory room free, with no account.

More Trees problems