A BST Iterator allows us to traverse 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 achieve O(h) space complexity, where h is the tree height?
It uses a stack to store nodes along the path from the root to the current node, pushing only left children. This stack size is at most the height of the tree, so space is O(h).
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 by popping the top node from the stack and then pushing all left children of its right subtree.
Click to reveal answer
intermediate
Why do we push left children onto the stack during BST Iterator initialization and after calling next()?
Because in-order traversal visits left subtree first, pushing left children ensures the smallest elements are on top of the stack, ready to be returned next.
Click to reveal answer
advanced
What is the time complexity of the next() method in a BST Iterator over n calls?
Amortized time complexity is O(1) per call because each node is pushed and popped exactly once during the entire iteration.
Click to reveal answer
What data structure is commonly used inside a BST Iterator to track nodes?
ALinked List
BQueue
CArray
DStack
✗ Incorrect
A stack is used to keep track of nodes along the path to the next smallest element.
What traversal order does a BST Iterator follow?
AIn-order
BPre-order
CPost-order
DLevel-order
✗ Incorrect
BST Iterator returns elements in sorted order using in-order traversal.
What does the hasNext() method in a BST Iterator check?
AIf the stack is empty
BIf the current node has a right child
CIf the stack is not empty
DIf the tree root is null
✗ Incorrect
hasNext() returns true if there are still nodes to visit, which means the stack is not empty.
When next() is called, after popping a node, what nodes are pushed onto the stack?
AAll right children of the popped node
BAll left children of the popped node's right subtree
CAll left children of the popped node's left subtree
DAll children of the popped node
✗ Incorrect
After popping, we push all left children of the right subtree to maintain in-order traversal.
What is the worst-case space complexity of a BST Iterator for a balanced BST?
AO(log n)
BO(n)
CO(1)
DO(n log n)
✗ Incorrect
For a balanced BST, the height h is O(log n), so space complexity is O(h) = O(log n).
Explain how a BST Iterator uses a stack to return elements in sorted order.
Think about in-order traversal and how the stack helps track nodes.
You got /4 concepts.
Describe the time and space complexity of the BST Iterator and why it is efficient.
Consider how often nodes are pushed/popped and the maximum stack size.
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 storing all elements at once
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 loading all nodes into memory at once.
Step 2: Compare options with BST Iterator purpose
Options B, C, and D relate to modifying the tree, not iterating over it. To traverse the BST in ascending order without storing all elements at once correctly describes the iterator's purpose.
Final Answer:
To traverse the BST in ascending order without storing all elements at once -> Option A
Quick Check:
BST Iterator = In-order traversal on demand [OK]
Hint: BST Iterator returns next smallest element step-by-step [OK]
Common Mistakes:
Confusing iterator with insertion or deletion operations
Thinking iterator balances the tree
Assuming iterator stores all nodes at once
2. Which of the following is the correct TypeScript method signature for the next() method in a BST Iterator class that returns the next smallest number?
easy
A. next(value: number): number
B. next(): void
C. next(): number
D. next(): boolean
Solution
Step 1: Understand the purpose of next()
The next() method should return the next smallest number in the BST, so it must return a number.
Step 2: Analyze method signatures
next(): number returns a number with no parameters, which fits the iterator pattern. next(): void returns void, so no value is returned. next(value: number): number takes a parameter, which is not needed. next(): boolean returns boolean, which is incorrect.
Final Answer:
next(): number -> Option C
Quick Check:
next() returns next smallest number [OK]
Hint: next() returns a number without parameters [OK]
Common Mistakes:
Adding unnecessary parameters to next()
Returning void instead of number
Returning boolean instead of number
3. Given the following BST Iterator usage in TypeScript, what will be the output printed?
B. In pushLeft, should traverse left child, not right
C. hasNext() should check if stack is empty incorrectly
D. Constructor should not call pushLeft
Solution
Step 1: Analyze pushLeft method
pushLeft should push all left children to stack to simulate in-order traversal. The code incorrectly moves to node.right instead of node.left.
Step 2: Check other methods
next() correctly pops from stack and pushes left children of right subtree. hasNext() correctly checks stack length. Constructor correctly calls pushLeft.
Final Answer:
In pushLeft, should traverse left child, not right -> Option B
Quick Check:
pushLeft must go left to simulate in-order [OK]
Hint: pushLeft must follow left children, not right [OK]
Common Mistakes:
Traversing right child in pushLeft instead of left
Misunderstanding stack usage in iterator
Incorrect pop direction in next()
5. You want to design a BST Iterator that supports peek() to see the next smallest element without advancing the iterator. Which approach best supports this feature efficiently?
hard
A. Use a queue to store nodes in BFS order
B. Store all BST elements in an array and use an index pointer
C. Traverse the BST every time peek() is called
D. Use a stack to store left children and keep the top as peek value
Solution
Step 1: Understand peek() requirement
peek() must return next smallest element without moving forward, so the next element must be accessible without popping.
Step 2: Evaluate approaches
Use a stack to store left children and keep the top as peek value uses a stack with left children; the top of the stack is the next smallest element, so peek() can return it efficiently. Store all BST elements in an array and use an index pointer stores all elements but uses extra memory. Traverse the BST every time peek() is called is inefficient as it traverses repeatedly. Use a queue to store nodes in BFS order uses BFS order, which is not sorted.
Final Answer:
Use a stack to store left children and keep the top as peek value -> Option D
Quick Check:
Stack top = next smallest for peek() [OK]
Hint: Stack top always holds next smallest for peek() [OK]
Common Mistakes:
Using BFS queue which is unordered
Re-traversing tree on every peek() call
Storing all elements wastes memory
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.