oneby7

Binary Search Trees Using the ordering

398. Binary Search Tree Iterator

Medium

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() returns true if some value has not been returned yet, and false otherwise. 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

37911152230

Input: root = [15, 7, 22, 3, 11, null, 30, null, null, 9] ops = ["next", "next", "hasNext", "next", "next", "next", "hasNext", "next", "next", "hasNext"]

Output: [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

−10⁹

Input: root = [-1000000000] ops = ["hasNext", "next", "hasNext", "hasNext"]

Output: [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

891012

Input: root = [8, null, 12, 10, null, 9] ops = ["next", "next", "hasNext", "hasNext", "next", "next"]

Output: [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.

root = [50, 30, 70, 20, 40, 60, 80] ops = ["next", "next", "next", "next", "next", "hasNext"]

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".
  • next is only called when a value is left.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.