173. Binary Search Tree Iterator
Binary Search Tree Iterator (LeetCode 173) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Inorder Traversal subpattern.
How to solve Binary Search Tree Iterator
Binary Search Tree Iterator is a Inorder Traversal problem, part of Tree Traversal Patterns (DFS & BFS). Master inorder traversal for BST validation and kth element problems. Produces sorted order in BST.
Work towards the solution in these steps:
- Recall that an in-order traversal of a BST yields the nodes in ascending order; think about how to traverse the tree incrementally rather than all at once.
- Consider using a stack data structure to simulate the recursive in-order traversal iteratively, which allows you to pause and resume traversal.
- Think about pushing all the left children onto the stack initially to reach the smallest element, and then process nodes by popping from the stack and pushing right children when moving forward.
The reference solution runs in O(1) amortized time and O(h) space.
The Binary Search Tree Iterator solution and editorial walks through the full approach with reference code.
Problem statement
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.
Example 1:
Input ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"] [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []] Output [null, 3, 7, true, 9, true, 15, true, 20, false]
Explanation BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]); bSTIterator.next(); // return 3 bSTIterator.next(); // return 7 bSTIterator.hasNext(); // return True bSTIterator.next(); // return 9 bSTIterator.hasNext(); // return True bSTIterator.next(); // return 15 bSTIterator.hasNext(); // return True bSTIterator.next(); // return 20 bSTIterator.hasNext(); // return False
Constraints:
- The number of nodes in the tree is in the range [1, 10^5].
- 0 <= Node.val <= 10^6
- At most 10^5 calls will be made to hasNext, and next.
Follow up:
- Could you implement next() and hasNext() to run in average O(1) time and use O(h) memory, where h is the height of the tree?
Topics and companies
Topics: Stack, Tree, Design, Binary Search Tree, Binary Tree, Iterator.
Reported in interviews at Facebook, ByteDance, Microsoft, Bloomberg, Google, Oracle, Amazon, Apple, Adobe.
How to practise Binary Search Tree Iterator on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(1) amortized time and O(h) space.
Open Binary Search Tree Iterator in the code editor, or read the Binary Search Tree Iterator editorial for a worked solution with its approach and complexity analysis.
Where Binary Search Tree Iterator sits in the DSA pattern sheet
- Tree Traversal Patterns (DFS & BFS) pattern guide
- Recursive Inorder Traversal subpattern
- The full DSA patterns sheet
Problems related to Binary Search Tree Iterator
Other problems that use the same Recursive Inorder Traversal and Tree Traversal Patterns (DFS & BFS) techniques:
- Binary Tree Inorder Traversal — Easy, same subpattern
- Delete Node in a BST — Medium, same subpattern
- Find Mode in Binary Search Tree — Easy, same subpattern
- Kth Smallest Element in a BST — Medium, same subpattern
- Minimum Absolute Difference in BST — Easy, same subpattern
- Validate Binary Search Tree — Medium, same subpattern
Binary Search Tree Iterator FAQ
What is the LeetCode number of Binary Search Tree Iterator?
Binary Search Tree Iterator is LeetCode problem 173, rated Medium.
What pattern is used to solve Binary Search Tree Iterator?
Binary Search Tree Iterator is a Inorder Traversal problem. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Inorder Traversal subpattern.
What is the time complexity of Binary Search Tree Iterator?
The reference solution runs in O(1) amortized time and O(h) space.
Which companies ask Binary Search Tree Iterator?
Binary Search Tree Iterator has been reported in interviews at Facebook, ByteDance, Microsoft, Bloomberg, Google, Oracle, Amazon, Apple, Adobe.