A BST Iterator allows you to traverse a Binary Search Tree (BST) in sorted order (in-order traversal) efficiently, providing next smallest elements one by one without traversing the entire tree at once.
Click to reveal answer
intermediate
How does a BST Iterator typically store its state internally?
It uses a stack to keep track of nodes. The stack stores the path from the root to the current node, allowing the iterator to return the next smallest element and move forward efficiently.
Click to reveal answer
beginner
Explain the role of the 'hasNext()' method in a BST Iterator.
'hasNext()' checks if there are more nodes to visit in the BST. It returns true if the stack is not empty, meaning there are still nodes left to traverse in sorted order.
Click to reveal answer
intermediate
What does the 'next()' method do in a BST Iterator?
'next()' returns the next smallest value in the BST. It pops the top node from the stack, processes it, and pushes all the left children of the popped node's right subtree onto the stack.
Click to reveal answer
intermediate
Why is a stack used instead of recursion in BST Iterator design?
Using a stack explicitly avoids recursion overhead and allows the iterator to pause and resume traversal efficiently, providing one element at a time without traversing the whole tree upfront.
Click to reveal answer
What traversal order does a BST Iterator follow to return elements?
APre-order traversal
BLevel-order traversal
CPost-order traversal
DIn-order traversal
✗ Incorrect
A BST Iterator returns elements in sorted order, which corresponds to in-order traversal of the BST.
What data structure is commonly used inside a BST Iterator to track nodes?
AQueue
BStack
CLinked List
DHash Map
✗ Incorrect
A stack is used to keep track of nodes during in-order traversal in a BST Iterator.
What does the 'hasNext()' method return when the BST Iterator has no more elements?
Afalse
Bnull
Ctrue
Dundefined
✗ Incorrect
'hasNext()' returns false when there are no more nodes left to traverse.
When 'next()' is called, which nodes are pushed onto the stack?
AAll left children of the popped node's right subtree
BAll nodes in the tree
CAll left children of the popped node
DAll right children of the popped node
✗ Incorrect
'next()' pushes all left children of the popped node's right subtree to maintain in-order traversal.
Why is the BST Iterator efficient for large trees?
AIt traverses the entire tree at once
BIt uses recursion to speed up traversal
CIt returns elements one by one without full traversal upfront
DIt stores all nodes in an array first
✗ Incorrect
The iterator returns elements one by one, using a stack to avoid full traversal upfront, saving time and memory.
Describe how a BST Iterator uses a stack to return the next smallest element.
Think about how in-order traversal visits nodes left-root-right.
You got /4 concepts.
Explain the difference between using recursion and a stack in BST Iterator design.
Consider how recursion and stack manage traversal state.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a BST Iterator in a binary search tree?
easy
A. To traverse the BST in ascending order without recursion
B. To insert new nodes into the BST
C. To delete nodes from the BST
D. To balance the BST automatically
Solution
Step 1: Understand BST Iterator functionality
A BST Iterator is designed to traverse the tree in sorted order, typically ascending, without using recursion.
Step 2: Identify the main purpose
It provides a way to get the next smallest element efficiently, which matches To traverse the BST in ascending order without recursion.
Final Answer:
To traverse the BST in ascending order without recursion -> Option A
Quick Check:
BST Iterator = Ascending traversal without recursion [OK]
Hint: BST Iterator returns next smallest element step-by-step [OK]
Common Mistakes:
Confusing iterator with insertion or deletion
Thinking it balances the tree automatically
Assuming it uses recursion internally
2. Which of the following is the correct way to initialize a BST Iterator class in JavaScript?
easy
A. function BSTIterator(root) { this.stack = []; this.pushRight(root); }
B. class BSTIterator { constructor(root) { this.stack = []; this.pushLeft(root); } }
C. class BSTIterator { constructor(root) { this.queue = []; this.pushLeft(root); } }
D. function BSTIterator(root) { this.queue = []; this.pushRight(root); }
Solution
Step 1: Identify correct class syntax and initialization
The BST Iterator is usually implemented as a class with a stack to store nodes and a helper method to push left children.
Step 2: Check each option
class BSTIterator { constructor(root) { this.stack = []; this.pushLeft(root); } } correctly uses a class, initializes a stack, and calls pushLeft on root. Others use queue or pushRight which are incorrect for BST Iterator.
Final Answer:
class BSTIterator { constructor(root) { this.stack = []; this.pushLeft(root); } } -> Option B
Quick Check:
Use stack and pushLeft in constructor [OK]
Hint: BST Iterator uses stack and pushLeft method [OK]
Common Mistakes:
Using queue instead of stack
Calling pushRight instead of pushLeft
Using function instead of class syntax
3. Given the BST below and the following code snippet, what will be the output?
BST structure:
```
7
/ \
3 15
/ \
9 20
```
Code:
const iterator = new BSTIterator(root);
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.hasNext());
medium
A. [3, 7, false]
B. [7, 9, true]
C. [3, 7, true]
D. [9, 15, true]
Solution
Step 1: Trace the first next() call
First next() returns the smallest element, which is 3.
Step 2: Trace the second next() call
Second next() returns the next smallest, which is 7.
Step 3: Check hasNext()
After two next() calls, there are still nodes (9, 15, 20), so hasNext() returns true.
Final Answer:
[3, 7, true] -> Option C
Quick Check:
next() returns ascending nodes, hasNext() true if more nodes [OK]
Hint: next() returns next smallest; hasNext() true if stack not empty [OK]
Common Mistakes:
Assuming next() returns root first
Confusing hasNext() return value
Ignoring left subtree nodes
4. Consider this buggy BST Iterator method. What is the main error?
B. next() should return node.key instead of node.val
C. pushRight should use a queue instead of stack
D. pushRight should push left children, not right
Solution
Step 1: Analyze pushRight method
pushRight pushes right children, but BST Iterator needs to push left children to get next smallest.
Step 2: Understand correct pushLeft usage
Correct method pushes left children of right subtree to stack to maintain ascending order.
Final Answer:
pushRight should push left children, not right -> Option D
Quick Check:
Push left children to stack for correct order [OK]
Hint: Push left children of right subtree, not right children [OK]
Common Mistakes:
Pushing right children instead of left
Using queue instead of stack
Returning wrong node property
5. You want to design a BST Iterator that supports next() and hasNext() in O(1) average time and uses O(h) space, where h is tree height. Which approach best achieves this?
hard
A. Use a stack to store left children and push left nodes of right subtree on next()
B. Store all nodes in an array by inorder traversal at initialization
C. Use recursion to traverse nodes on each next() call
D. Use a queue to store nodes level by level
Solution
Step 1: Understand time and space constraints
O(1) average time per next() and O(h) space means no full traversal storage and no recursion per call.
Step 2: Evaluate each approach
Use a stack to store left children and push left nodes of right subtree on next() uses a stack to store left children and pushes left nodes of right subtree on next(), achieving O(1) average time and O(h) space. Store all nodes in an array by inorder traversal at initialization uses O(n) space. Use recursion to traverse nodes on each next() call uses recursion, not O(1) time. Use a queue to store nodes level by level uses queue level order, not inorder.
Final Answer:
Use a stack to store left children and push left nodes of right subtree on next() -> Option A
Quick Check:
Stack + push left children = O(1) avg time, O(h) space [OK]
Hint: Use stack with left children for efficient BST Iterator [OK]
Common Mistakes:
Storing all nodes wastes space
Using recursion causes slow next()
Using queue gives wrong traversal order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.