This question evaluates understanding of binary tree traversal and iterator design, specifically the implementation of a lazy inorder iterator under target space and amortized time constraints; it belongs to the Coding & Algorithms category and emphasizes practical implementation skills.
Design an iterator over a binary tree that returns nodes in inorder sequence (left → root → right) using lazy traversal (i.e., you may not precompute/store the entire inorder list).
Implement a class with the following operations:
InorderIterator(root)
: initialize the iterator for the given binary tree root.
boolean hasNext()
: returns
true
if there is a next node in inorder order.
TreeNode next()
: returns the next node (or its value) in inorder order.
O(h)
where
h
is the tree height.
O(1)
per
next()
.