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 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 move to the next smallest element efficiently.
Click to reveal answer
intermediate
Explain the role of the 'next()' function in a BST Iterator.
The 'next()' function returns the next smallest element in the BST. It pops the top node from the stack, processes it, and then pushes all the left children of the popped node's right child onto the stack.
Click to reveal answer
beginner
Why does the BST Iterator push left children onto the stack during initialization and after visiting a node?
Because in-order traversal visits left children first, pushing left children ensures the smallest elements are on top of the stack, ready to be returned by 'next()'.
Click to reveal answer
intermediate
What is the time complexity of the 'next()' and 'hasNext()' operations in a BST Iterator?
'hasNext()' runs in O(1) time. 'next()' runs in average O(1) amortized time because each node is pushed and popped exactly once.
Click to reveal answer
What data structure is commonly used inside a BST Iterator to track nodes?
AStack
BQueue
CArray
DLinked List
✗ Incorrect
A stack is used to keep track of nodes along the path to the current node for in-order traversal.
What does the 'next()' method of a BST Iterator return?
AThe last visited node value
BThe next smallest element in the BST
CThe root node value
DThe next largest element in the BST
✗ Incorrect
'next()' returns the next smallest element following in-order traversal.
When initializing a BST Iterator, which nodes are pushed onto the stack first?
AAll left children from the root down
BAll leaf nodes
CAll right children
DOnly the root node
✗ Incorrect
All left children from the root down to the leftmost node are pushed to start with the smallest element.
What is the average time complexity of the 'next()' operation in a BST Iterator?
AO(n)
BO(log n)
CO(1) amortized
DO(n log n)
✗ Incorrect
'next()' runs in O(1) amortized time because each node is pushed and popped once.
What does the 'hasNext()' method indicate in a BST Iterator?
AIf there is a next larger element
BIf the current node has a right child
CIf the BST is empty
DIf the stack is not empty, meaning more elements remain
✗ Incorrect
'hasNext()' returns true if the stack is not empty, meaning there are more nodes to visit.
Describe how a BST Iterator uses a stack to perform in-order traversal step-by-step.
Think about how in-order traversal visits nodes left-root-right.
You got /4 concepts.
Explain why the BST Iterator's next() method has O(1) amortized time complexity.
Consider how many times each node is handled during iteration.
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 = In-order traversal without recursion [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 way to declare a stack to be used in a BST Iterator in C++?
easy
A. stack s;
B. stack<int> s;
C. stack<TreeNode> s;
D. stack s;
Solution
Step 1: Identify the data type stored in the stack
The BST Iterator stores pointers to tree nodes to traverse the tree efficiently, so the stack should hold TreeNode pointers.
Step 2: Match the correct syntax
stack s; uses stack which is correct. stack<int> s; uses int, which is incorrect. stack<TreeNode> s; uses TreeNode by value, which is inefficient. stack s; uses Node*, which is undefined here.
Final Answer:
stack s; -> Option D
Quick Check:
Stack stores pointers to TreeNode [OK]
Hint: Stack must hold pointers to tree nodes, not values [OK]
Common Mistakes:
Using stack of int instead of TreeNode*
Using stack of TreeNode by value causing copies
Using undefined Node* type
3. Given the following BST Iterator code snippet, what will be the output sequence of calls to next()?
Step 1: Understand the BST structure and in-order traversal
The BST is:
7
/ \
3 15
/ \
9 20
In-order traversal visits nodes in ascending order: 3, 7, 9, 15, 20.
Step 2: Trace the iterator's next() calls
Iterator pushes left nodes first, so initial stack: 7, 3 (top). Calls to next() pop 3, then 7, then push left of 7's right (15), then 9, then 15, then 20. Output sequence matches 3 7 9 15 20.
Final Answer:
3 7 9 15 20 -> Option C
Quick Check:
In-order traversal = 3 7 9 15 20 [OK]
Hint: BST Iterator outputs nodes in ascending order (in-order) [OK]
Common Mistakes:
Confusing pre-order or post-order traversal output
Not pushing left children correctly
Misunderstanding stack top element order
4. Identify the bug in the following BST Iterator next() method and select the correct fix:
int next() {
TreeNode* node = s.top();
s.pop();
pushLeft(node->left); // Bug here
return node->val;
}
medium
A. Remove pushLeft call completely
B. Change pushLeft(node->left) to pushLeft(node->right)
C. Change s.pop() to s.pop(node)
D. Add check if node is nullptr before pushLeft
Solution
Step 1: Analyze the next() method logic
After popping the current node, the iterator should push all left children of the right subtree to continue in-order traversal.
Step 2: Identify the incorrect call
The code incorrectly calls pushLeft(node->left), which revisits the left subtree already processed. It should call pushLeft(node->right) to process the right subtree.
Final Answer:
Change pushLeft(node->left) to pushLeft(node->right) -> Option B
Quick Check:
Push left of right subtree after pop [OK]
Hint: Push left nodes of right child after popping [OK]
5. You want to design a BST Iterator that supports a method peek() which returns the next smallest element without advancing the iterator. How would you modify the existing BST Iterator design to support peek() efficiently?
hard
A. Store the next smallest element in a variable during next() and return it in peek()
B. Use an additional stack to store peeked elements separately
C. Modify next() to not pop from stack and peek() to pop instead
D. Precompute all elements in sorted order and store in a vector
Solution
Step 1: Understand peek() requirement
peek() should return next smallest element without moving the iterator forward, so it must remember the next element.
Step 2: Modify iterator to store next element
By storing the next smallest element in a variable during next(), peek() can return this stored value without popping or changing the stack.
Step 3: Evaluate other options
Using an extra stack or precomputing all elements wastes space or changes iterator behavior. Modifying next() to not pop breaks standard iteration.
Final Answer:
Store the next smallest element in a variable during next() and return it in peek() -> Option A
Quick Check:
Cache next element for peek() [OK]
Hint: Cache next element on next() call for peek() [OK]
Common Mistakes:
Precomputing all elements wastes memory
Changing next() behavior breaks iteration
Using extra stack complicates design unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.