Problem · Tree
Binary Search Tree Iterator
Learn this problemProblem statement
Process next and hasNext operations against one ascending BST iterator. next returns the next smallest value; hasNext reports whether another value remains. Return each result as a string in operation order.
Function
runBSTIterator(root: TreeNode, operations: String[]) → String[]Examples
Example 1
root = [7,3,15,null,null,9,20]operations = ["next","next","hasNext","next","hasNext"]return = ["3","7","true","9","true"]The iterator yields the inorder sequence and keeps its position between operations.
Constraints
- The BST contains between
1and10000unique nodes. - Every
nextoperation is valid. - There are at most
20000operations.