BST Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. The first call to next() will return the smallest number in BST. Calling next() again will return the next smallest number in the BST, and so on.

Using Recursive Inorder Traversal

Construct the inorder of the given tree and store it into a vector. Keep an index variable to point at the elements in this vector.

Time Complexity: O(n)O(n)​

Space Complexity: O(n)O(n)

Using Stack

Time Complexity: O(h)O(h)​

Space Complexity: O(h)O(h)

Last updated