Binary Search Tree Iterator (LC #173) implements next() and hasNext() with average time. Use controlled in-order traversal with an explicit stack. In the constructor, push all left children of root. For next(), pop the top, push all left children of its right child, return the popped value. hasNext() checks if the stack is empty. Each node is pushed and popped exactly once, so amortized per operation. Space is for the stack. This pattern appears in many iterator design problems.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/meta-interview-prep/trees/binary-search-tree-iterator
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████████████████████████████████████████████████████████████████████████████████