A BST Iterator allows traversing a Binary Search Tree (BST) in sorted order (in-order traversal) one element at a time, without storing all elements at once.
Click to reveal answer
intermediate
How does a BST Iterator typically achieve in-order traversal without recursion?
It uses a stack to keep track of nodes. It pushes left children down to the stack, then visits nodes and moves to right children, simulating recursion iteratively.
Click to reveal answer
beginner
In BST Iterator design, what does the 'next()' method do?
The 'next()' method returns the next smallest element in the BST and advances the iterator to the following element.
Click to reveal answer
intermediate
What is the time complexity of 'next()' and 'hasNext()' methods in a well-designed BST Iterator?
'next()' and 'hasNext()' both run in average O(1) time, with occasional O(h) time where h is the tree height, due to stack operations.
Click to reveal answer
intermediate
Why is a stack used in BST Iterator design instead of storing all elements in a list?
Using a stack saves memory by only storing nodes needed for the current traversal path, avoiding the need to store the entire BST elements upfront.
Click to reveal answer
What data structure is commonly used inside a BST Iterator to simulate in-order traversal?
AArray
BQueue
CStack
DHashMap
✗ Incorrect
A stack is used to keep track of nodes during in-order traversal without recursion.
What does the 'hasNext()' method in a BST Iterator check?
AIf the current node has a right child
BIf the BST is empty
CIf the stack is empty
DIf there is a next smallest element available
✗ Incorrect
'hasNext()' returns true if there is a next smallest element to return.
What is the order of elements returned by a BST Iterator?
APre-order
BIn-order
CPost-order
DLevel-order
✗ Incorrect
BST Iterator returns elements in sorted order using in-order traversal.
Which of these is true about the space complexity of a BST Iterator using a stack?
AO(h) where h is tree height
BO(n) where n is total nodes
CO(1) constant space
DO(log n) always
✗ Incorrect
The stack stores nodes along the path, so space is proportional to tree height h.
When 'next()' is called, what does the BST Iterator do after returning the current node's value?
APushes all left children of the right subtree onto the stack
BClears the stack
CMoves to the root node
DReturns null
✗ Incorrect
After returning a node, it pushes all left children of that node's right subtree to the stack.
Explain how a BST Iterator uses a stack to perform in-order traversal step-by-step.
Think about how recursion is simulated using a stack.
You got /4 concepts.
Describe the advantages of using a BST Iterator over converting the BST to a sorted list first.
Consider memory and performance benefits.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a BST Iterator in the context of 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 explicitly.
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 [OK]
Hint: BST Iterator returns next smallest element in sorted order [OK]
Common Mistakes:
Confusing iterator with insertion or deletion operations
Thinking it balances the tree automatically
Assuming it uses recursion internally
2. Which of the following is the correct method signature for the next() method in a BST Iterator class in Java?
easy
A. public Node next()
B. public void next()
C. public int next()
D. public boolean next()
Solution
Step 1: Understand the return type of next()
The next() method returns the next smallest integer value from the BST, so it should return an int.
Step 2: Match method signature
public int next() matches the expected signature: public int next().
Final Answer:
public int next() -> Option C
Quick Check:
next() returns int value [OK]
Hint: next() returns next smallest int value from BST [OK]
Common Mistakes:
Using void return type for next()
Returning boolean instead of int
Returning Node instead of int value
3. Given the following BST Iterator code snippet, what will be the output sequence of next() calls?
Each next() call returns the next smallest element, so the output is 3, then 7, then 9.
Final Answer:
3, 7, 9 -> Option B
Quick Check:
In-order traversal = 3,7,9 [OK]
Hint: next() returns ascending BST values in order [OK]
Common Mistakes:
Assuming next() returns descending order
Mixing up node values order
Confusing next() with hasNext()
4. Identify the bug in this BST Iterator next() method implementation:
public int next() {
TreeNode node = stack.pop();
if (node.right != null) {
pushLeft(node.right);
}
return node.val;
}
private void pushLeft(TreeNode node) {
while (node != null) {
stack.push(node);
node = node.left;
}
}
medium
A. The next() method should return node.right.val instead of node.val
B. The pushLeft method should push right children instead of left
C. The stack is not initialized before use
D. The next() method should check if stack is empty before popping
Solution
Step 1: Analyze next() method stack usage
Calling stack.pop() without checking if stack is empty can cause runtime error if no elements remain.
Step 2: Identify missing safety check
Proper implementation should check stack emptiness before popping to avoid exceptions.
Final Answer:
The next() method should check if stack is empty before popping -> Option D
Quick Check:
Check stack empty before pop [OK]
Hint: Always check stack not empty before pop in next() [OK]
Common Mistakes:
Ignoring stack empty check causing exceptions
Misunderstanding pushLeft logic
Returning wrong node value
5. You want to design a BST Iterator that supports peek() to see the next smallest element without advancing the iterator. Which approach correctly extends the standard BST Iterator design?
hard
A. Store the next smallest element in a variable during next() and return it in peek() without popping stack
B. Use an extra stack to store all nodes and pop from it in peek()
C. Modify next() to return the top of the stack without popping it, and peek() pops the stack
D. Traverse the entire BST in constructor and store values in a list for peek() and next()
Solution
Step 1: Understand peek() requirement
peek() should return next smallest element without advancing iterator, so it must not pop stack.
Step 2: Identify correct approach
Storing next smallest element in a variable during next() allows peek() to return it without changing stack state, matching Store the next smallest element in a variable during next() and return it in peek() without popping stack.
Final Answer:
Store the next smallest element in a variable during next() and return it in peek() without popping stack -> Option A
Quick Check:
peek() returns next without advancing [OK]
Hint: Cache next element to support peek() without stack pop [OK]
Common Mistakes:
Popping stack in peek() advancing iterator
Using extra stack unnecessarily
Precomputing all values losing iterator benefits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.