Binary Search Tree Iterator - Problem

Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST):

  • BSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.
  • boolean hasNext() Returns true if there exists a number in the traversal to the right of the pointer, otherwise returns false.
  • int next() Moves the pointer to the right, then returns the number at the pointer.

Notice that by initializing the pointer to a non-existent smallest number, the first call to next() will return the smallest element in the BST.

You may assume that next() calls will always be valid. That is, there will be at least a next number in the in-order traversal when next() is called.

Input & Output

Example 1 — Basic BST Iterator
$ Input: commands = ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"], values = [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
Output: [null, 3, 7, true, 9, true, 15, true, 20, false]
💡 Note: Initialize iterator with BST root 7. In-order traversal gives 3,7,9,15,20. Each next() returns the next smallest value, hasNext() checks if more values exist.
Example 2 — Single Node Tree
$ Input: commands = ["BSTIterator", "next", "hasNext"], values = [[[5]], [], []]
Output: [null, 5, false]
💡 Note: Tree with single node 5. First next() returns 5, then hasNext() returns false as no more values exist.
Example 3 — Left Skewed Tree
$ Input: commands = ["BSTIterator", "next", "next", "hasNext"], values = [[[3, 1, null, null, 2]], [], [], []]
Output: [null, 1, 2, true]
💡 Note: Left-skewed BST. In-order traversal: 1,2,3. After getting 1 and 2, hasNext() is true because 3 remains.

Constraints

  • The number of nodes in the tree is in the range [1, 105]
  • 0 ≤ Node.val ≤ 106
  • At most 105 calls will be made to hasNext, and next

Visualization

Tap to expand
Binary Search Tree Iterator INPUT Binary Search Tree 7 3 15 9 20 In-order: 3, 7, 9, 15, 20 Commands: BSTIterator, next, next, hasNext, next, hasNext, next, hasNext, next, hasNext root = [7, 3, 15, null, null, 9, 20] ALGORITHM STEPS 1 Initialize Stack Push all left nodes from root 3 7 Stack 2 next() Operation Pop top, push left of right child nodes to stack 3 hasNext() Check Return true if stack is not empty 4 Controlled Traversal Lazy evaluation: process nodes on demand Time: O(1) avg | Space: O(h) h = height of tree FINAL RESULT Operation Results: Call Output BSTIterator null next() 3 next() 7 hasNext() true next() 9 hasNext() true next() 15 hasNext() true next() 20 hasNext() false Output Array: [null,3,7,true,9,true,15,true,20,false] Key Insight: Use a stack to simulate in-order traversal. Push all left children initially. On next(), pop and return the top, then push all left children of the right subtree. This gives O(1) average time per operation with O(h) space. TutorialsPoint - Binary Search Tree Iterator | Optimal Stack-Based Solution
Asked in
Facebook 35 Google 28 Amazon 22 Microsoft 18
157.6K Views
Medium-High Frequency
~25 min Avg. Time
3.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen