#K91672. BST Iterator

    ID: 38028 Type: Default 1000ms 256MiB

BST Iterator

BST Iterator

Given a binary search tree (BST), implement an iterator that returns the elements of the BST in ascending order (i.e. an in-order traversal).

The iterator should support the following operations:

  • next(): Returns the next smallest number in the BST.
  • hasNext(): Returns whether there is a next smallest number available.

You will be given the BST as a level-order input (with the token null representing a missing node). Your task is to build the BST, initialize the iterator, and then output all elements in ascending order. The expected average time complexity for next() is O(1) across all calls.

Note: Use an iterative approach with a stack to implement the iterator.

inputFormat

The input is provided via standard input (stdin) as a single line. The line contains space-separated tokens representing the level order traversal of the BST. Each token is either an integer or the string null indicating a missing node.

For example: 7 3 15 null null 9 20

outputFormat

Output via standard output (stdout) a single line containing the in-order (ascending) sequence of node values separated by a single space.

For example: 3 7 9 15 20

## sample
7 3 15 null null 9 20
3 7 9 15 20