AlgoInsight

DSA problem

Binary Search Tree Iterator

MediumTree Traversal - In OrderPattern: Tree Traversal - In Ordertop150MetaAmazonMicrosoftGoogleLinkedInBloomberg

Implement the BSTIterator class that represents an iterator over the inorder traversal of a Binary Search Tree (BST):

BSTIterator(TreeNode root) initializes the iterator. The pointer should be initialized to a non-existent number smaller than any element in the BST. next() moves the pointer to the right (next smallest) number and returns that number. hasNext() returns true if there exists a number on the right side of the pointer, otherwise false.

The naive implementation is to perform a full inorder traversal in the constructor, store every value in a list, and serve next() by index. That is correct but uses O(n) memory upfront, regardless of how many calls the consumer will make. The interview goal is to achieve O(1) memory beyond the input — measured in non-recursive auxiliary storage — and to have next() return in O(1) amortized time.

The optimal approach is a controlled iterative inorder traversal. In the constructor, push the chain of leftmost ancestors of the root onto a stack — this is exactly the prefix that a normal inorder traversal would push before producing its first value. Each call to next() pops the top of the stack to return its value, then pushes the leftmost chain of that node's right child to set up the following call.

Pushing the leftmost chain takes O(h) work in the worst case, but each node is pushed and popped at most once over the lifetime of the iterator, so across n calls to next() the total work is O(n) — that is amortized O(1) per call. The stack never holds more than h nodes at a time, giving O(h) auxiliary space rather than O(n).

Examples

Example 1
Input: ["BSTIterator","next","next","hasNext","next","hasNext","next","hasNext","next","hasNext"] [[[7,3,15,null,null,9,20]],[],[],[],[],[],[],[],[],[]]
Output: [null,3,7,true,9,true,15,true,20,false]
Explanation: After constructing the iterator on the BST [7,3,15,null,null,9,20], successive next() calls return 3,7,9,15,20 — the inorder sequence — and hasNext() correctly reports whether more values remain.
Example 2
Input: Constructor on [2,1,3]; then next(), next(), next(), hasNext()
Output: 1,2,3,false
Explanation: The inorder sequence is 1,2,3. Three next() calls return them in order, after which hasNext() returns false.
Example 3
Input: Constructor on [1]; then hasNext(), next(), hasNext()
Output: true,1,false
Explanation: The single value is 1. hasNext() reports true initially, next() returns 1, and then hasNext() reports false.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

bstdesigntree-traversalinorderiterator

Browse all DSA problems