Binary Search Trees Using the ordering
398. Binary Search Tree Iterator
You are given root, the root node of a binary search tree. Design an iterator that hands out the tree's values
one at a time, from smallest to largest. Write a class BSTIterator with a constructor and two methods:
BSTIterator(root)creates the iterator for the tree. No value has been returned yet.next()returns the smallest value that has not been returned yet. The first call returns the smallest value in the tree.hasNext()returnstrueif some value has not been returned yet, andfalseotherwise. It does not move the iterator.
next() is only called when a value is left, that is, when hasNext() would return true.
In a binary search tree, every value in a node's left subtree is smaller than the node's value, and every value in its right subtree is larger. The values in the tree are distinct.
Your class is tested by replaying a list of calls on a new BSTIterator(root). ops[i] names the i-th call,
"next" or "hasNext". The answer is the list of every value the calls return, in order: a number for each
next and true or false for each hasNext.
In C# and Go the methods are Next and HasNext, and in Rust hasNext is has_next. Go creates the iterator
with Constructor(root), and Rust with BSTIterator::new(root). The names in ops are the same in every
language.
The examples write a tree level by level, from left to right, with null where a child is missing. So
root = [5,8,null,3] is the root 5 with a left child 8 and no right child, and 8 has a left child 3. The
missing children after the last node are left out.
Example 1
Input: root = [15, 7, 22, 3, 11, null, 30, null, null, 9]
ops = ["next",
"next",
"hasNext",
"next",
"next",
"next",
"hasNext",
"next",
"next",
"hasNext"]
[3,7,true,9,11,15,true,22,30,false]Explanation: From smallest to largest, the values are 3, 7, 9, 11, 15, 22, 30, and the calls to next return them in that
order. hasNext is true while values are left, and false after 30, the last one, is returned.
Example 2
Input: root = [-1000000000]
ops = ["hasNext",
"next",
"hasNext",
"hasNext"]
[true,-1000000000,false,false]Explanation: The tree has one value, so the first hasNext is true. After next returns it, nothing is left. Calling
hasNext again does not change anything, so it is false both times.
Example 3
Input: root = [8, null, 12, 10, null, 9]
ops = ["next",
"next",
"hasNext",
"hasNext",
"next",
"next"]
[8,9,true,true,10,12]Explanation: After the root 8, the next value is 9, at the bottom of the left side of 8's right subtree, not 12. The two
calls to hasNext do not move the iterator, so the next call to next returns 10.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ number of nodes ≤ 105-109 ≤ Node.val ≤ 109- The tree has at most 1000 levels.
- The tree is a binary search tree, and its values are distinct.
1 ≤ ops.length ≤ 2 × 105- Each
ops[i]is"next"or"hasNext". nextis only called when a value is left.
Clarifying questions
What you would ask an interviewer before writing any code.