A BST Iterator allows us to traverse a Binary Search Tree (BST) in sorted order (in-order traversal) efficiently, providing next smallest elements one by one without traversing the whole tree at once.
Click to reveal answer
beginner
In BST Iterator design, why do we use a stack?
We use a stack to simulate the in-order traversal iteratively. The stack keeps track of nodes to visit next, allowing us to return the next smallest element efficiently without recursion.
Click to reveal answer
intermediate
What does the 'Next()' method do in a BST Iterator?
The 'Next()' method returns the next smallest element in the BST. It pops the top node from the stack, processes it, and pushes all left descendants of the popped node's right child onto the stack.
Click to reveal answer
beginner
How does the 'HasNext()' method work in a BST Iterator?
'HasNext()' checks if there are more nodes to visit by verifying if the stack is not empty. If the stack has nodes, it means there are still elements left in the BST to iterate over.
Click to reveal answer
intermediate
What is the time complexity of 'Next()' and 'HasNext()' methods in a BST Iterator?
'Next()' and 'HasNext()' methods both run in average O(1) time. Although 'Next()' may take O(h) time in worst case (h = tree height), amortized over all nodes, it is O(1). 'HasNext()' is always O(1).
Click to reveal answer
What data structure is commonly used inside a BST Iterator to track nodes?
AStack
BQueue
CLinked List
DArray
✗ Incorrect
A stack is used to simulate the in-order traversal iteratively by storing nodes to visit next.
What traversal order does a BST Iterator follow to return elements?
ALevel-order
BPost-order
CPre-order
DIn-order
✗ Incorrect
BST Iterator returns elements in sorted order using in-order traversal.
What does the 'HasNext()' method return when the stack is empty?
ATrue
BFalse
CThrows error
DReturns null
✗ Incorrect
'HasNext()' returns false when the stack is empty, meaning no more elements to iterate.
When 'Next()' is called, what nodes are pushed onto the stack after popping the top node?
AAll left child's right descendants
BAll left child's left descendants
CAll right child's left descendants
DAll right child's right descendants
✗ Incorrect
After popping, 'Next()' pushes all left descendants of the popped node's right child onto the stack.
What is the average time complexity of the 'Next()' method in a BST Iterator?
AO(1)
BO(h)
CO(n)
DO(log n)
✗ Incorrect
'Next()' runs in average O(1) time amortized over all nodes, even though worst case is O(h).
Explain how a BST Iterator uses a stack to return the next smallest element.
Think about simulating in-order traversal without recursion.
You got /4 concepts.
Describe the roles of 'Next()' and 'HasNext()' methods in a BST Iterator.
Focus on how these methods interact with the stack.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a BST Iterator in data structures?
easy
A. To insert new nodes into the BST
B. To balance the BST automatically
C. To delete nodes from the BST
D. To traverse the BST in ascending order without recursion
Solution
Step 1: Understand BST Iterator functionality
A BST Iterator is designed to traverse the tree in sorted order, usually ascending, without using recursion explicitly.
Step 2: Identify the main purpose
Among the options, only traversing in ascending order without recursion matches the BST Iterator's role.
Final Answer:
To traverse the BST in ascending order without recursion -> Option D
Quick Check:
BST Iterator = In-order traversal without recursion [OK]
Hint: BST Iterator helps traverse nodes in sorted order easily [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 C# method signature for the Next() method in a BST Iterator class that returns the next smallest integer?
easy
A. public string Next()
B. public void Next()
C. public int Next()
D. public bool Next()
Solution
Step 1: Determine the return type of Next()
The Next() method should return the next smallest integer value from the BST, so its return type must be int.
Step 2: Check method signature correctness
Only public int Next() matches the expected signature returning an integer value.
Final Answer:
public int Next() -> Option C
Quick Check:
Next() returns int value [OK]
Hint: Next() returns the next smallest value as int [OK]
Common Mistakes:
Using void return type for Next()
Returning bool instead of int
Returning string instead of int
3. Given the BST with nodes inserted in this order: 7, 3, 15, 9, 20. What is the output sequence of calling Next() five times on a correctly implemented BST Iterator?
medium
A. [7, 3, 15, 9, 20]
B. [3, 7, 9, 15, 20]
C. [20, 15, 9, 7, 3]
D. [3, 9, 7, 15, 20]
Solution
Step 1: Construct BST and find in-order traversal
Inserting 7, 3, 15, 9, 20 creates BST: 7 is root 3 left of 7 15 right of 7 9 left of 15 20 right of 15 In-order traversal (left-root-right) is [3, 7, 9, 15, 20].
Step 2: Understand Next() output sequence
Calling Next() five times returns nodes in in-order sequence: 3, 7, 9, 15, 20.
Hint: Next() outputs nodes in ascending order [OK]
Common Mistakes:
Confusing insertion order with traversal order
Reversing the output sequence
Mixing left and right subtree nodes
4. Identify the bug in this BST Iterator Next() method snippet and choose the correct fix:
public int Next() {
TreeNode node = stack.Pop();
if (node.right != null) {
stack.Push(node.right);
}
return node.val;
}
medium
A. Push all left children of node.right onto stack, not just node.right
B. Pop from stack only if it is not empty
C. Return node.val before popping from stack
D. Check node.left instead of node.right
Solution
Step 1: Analyze the Next() method logic
After popping node, if node.right exists, we must push node.right and all its left descendants to maintain in-order traversal.
Step 2: Identify missing step
Current code pushes only node.right, missing its left subtree nodes, causing incorrect traversal order.
Final Answer:
Push all left children of node.right onto stack, not just node.right -> Option A
Quick Check:
Push left subtree nodes after right child [OK]
Hint: Always push left children of right subtree after popping [OK]
Common Mistakes:
Pushing only right child without left descendants
Returning value before popping
Checking left child instead of right child
5. You want to design a BST Iterator that supports HasNext() and Next() methods with O(1) average time per call and O(h) space, where h is tree height. Which approach best achieves this?
hard
A. Use a stack to store left children nodes and push left descendants of right child on Next()
B. Store all nodes in a list during initialization and iterate over the list
C. Use recursion inside Next() to find next node each time
D. Use a queue to store nodes in level order traversal
Solution
Step 1: Understand time and space constraints
O(1) average time per call and O(h) space means we cannot store all nodes upfront or use recursion each call.
Step 2: Evaluate approaches
Using a stack to store left children and pushing left descendants of right child on Next() achieves these constraints efficiently.
Final Answer:
Use a stack to store left children nodes and push left descendants of right child on Next() -> Option A
Quick Check:
Stack with left children = O(1) avg time, O(h) space [OK]
Hint: Stack with left children keeps O(h) space and O(1) average time [OK]
Common Mistakes:
Storing all nodes wastes O(n) space
Using recursion causes higher time complexity
Using queue breaks in-order traversal order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.