927. BST Iterator (In-Order Lazy Evaluation)

MediumTreesBSTStackDesign

Design an iterator over a binary search tree that returns keys in ascending order. Simulate repeatedly calling next() until hasNext() is false, and return the full sequence of keys produced, as an array. The tree is given as a level-order BST array.

Input: A level-order BST array.

Output: Array — the keys in ascending order.

Examples

Example 1
Input: [7,3,15,null,null,9,20]
Output: [3,7,9,15,20]
Explanation: Inorder sequence from the iterator.
Example 2
Input: [5]
Output: [5]
Explanation: Single key.
Example 3
Input: []
Output: []
Explanation: Empty tree.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →