← 返回 apple 的题目列表Lazy Inorder Traversal Iterator for a Binary Tree
类型:online_judge
Problem
Implement an inorder traversal iterator InorderIterator for a binary tree that returns node values in Left → Node → Right order lazily (you must not store all nodes in advance).
Implement:
__init__(root): initialize with the root.
hasNext() -> bool: return true if there is a next node.
next() -> int: return the next node value and advance the iterator.
Requirements
Lazy traversal: do not precompute/store the full traversal.
Extra space should be O(h) where h is tree height.
Amortized time for next() should be O(1).
Input/Output (one possible judge format)
Input: a binary tree in level-order with null for missing nodes, plus a sequence of operations init, hasNext, next.
Output: print results for each hasNext/next.
If your platform does not support interactive class design, implement an equivalent driver that simulates the operations.
Constraints
0 <= N <= 2 * 10^5
Node values are 32-bit integers
Example
Input
Tree: [2,1,3]
Ops: init, hasNext, next, hasNext, next, hasNext, next, hasNext
Output
true
1
true
2
true
3
false