Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST):
BSTIterator(TreeNode root)Initializes an object of theBSTIteratorclass. Therootof the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.boolean hasNext()Returnstrueif there exists a number in the traversal to the right of the pointer, otherwise returnsfalse.int next()Moves the pointer to the right, then returns the number at the pointer.
Notice that by initializing the pointer to a non-existent smallest number, the first call to next() will return the smallest element in the BST.
You may assume that next() calls will always be valid. That is, there will be at least a next number in the in-order traversal when next() is called.
solutions
recursive inorder traversal
Just do a recursive inorder traversal and save the list.
class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.arr = [None]
self.idx = 0
self._to_list(root)
def _to_list(self, node):
if not node:
return
self._to_list(node.left)
self.arr.append(node.val)
self._to_list(node.right)
def next(self) -> int:
self.idx += 1
return self.arr[self.idx]
def hasNext(self) -> bool:
return self.idx < len(self.arr)-1