Binary Search Tree Iterator
Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST). The class should have a constructor that takes the root of a BST, a next() method that returns the next smallest number, and a hasNext() method that returns whether there is a next number.

30:00

Binary Search Tree Iterator
medium
Topics
Companies

Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST). The class should have a constructor that takes the root of a BST, a next() method that returns the next smallest number, and a hasNext() method that returns whether there is a next number.

Example 1:
Input: {"commands":["BSTIterator","next","next","hasNext","next","hasNext","next","hasNext","next","hasNext"],"values":[[[7,3,15,null,null,9,20]],[],[],[],[],[],[],[],[],[]]}
Output: [null,3,7,true,9,true,15,true,20,false]
Constraints:
  • The number of nodes is in the range [1,105][1, 10^5].

  • 0Node.val1060 \leq \text{Node.val} \leq 10^6

  • At most 10510^5 calls will be made to hasNext and next.

Input
arr ={"commands":["BSTIterator","next","next","hasNext","next","hasNext","next","hasNext","next","hasNext"],"values":[[[7,3,15,null,null,9,20]],[],[],[],[],[],[],[],[],[]]}

Initialize iterator. Push all left nodes to stack: [7, 3]

BST (level order)
7
3
15
9
20
Stack (top at right)
7
3
constructor
Results
null
Variables
No variables to display
DepthFunction Call
Stack empty
0/10