Recall & Review
beginner
What is the main purpose of a BST Iterator?
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?
✗ 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?
✗ 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?
✗ Incorrect
'hasNext()' returns false when there are no more nodes left to traverse.
When 'next()' is called, which nodes are pushed onto the stack?
✗ 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?
✗ 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.