Problem · Tree

Binary Search Tree Iterator

Learn this problem
MediumAtlassian logoAtlassianFULLTIMEOA

Problem 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 1 and 10000 unique nodes.
  • Every next operation is valid.
  • There are at most 20000 operations.

More Atlassian problems

drafts saved locally
public String[] runBSTIterator(TreeNode root, String[] operations) {
  // Write your code here.
}
root[7,3,15,null,null,9,20]
operations["next","next","hasNext","next","hasNext"]
expected["3", "7", "true", "9", "true"]
checking account