0
0
DSA Typescriptprogramming~5 mins

BST Iterator Design in DSA Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the main purpose of a BST Iterator?
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
What traversal order does a BST Iterator follow?
AIn-order
BPre-order
CPost-order
DLevel-order
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
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
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)
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.