In the problem Binary Tree Inorder Traversal, we solved it by simlating the function call.
Only a bit different this time,
when the caller calls next()
:
- just execute to next value returns
- save the stack and next address (state)
- yield the node value and return control to caller
actions above is the while
body part in the Binary Tree Inorder Traversal.
like Binary Tree Inorder Traversal, when the stack is empty, there are no more nodes.
However, if only one state left in the stack and the address of the state is POST
,
we need some additional check.
POST
state here means to deal with right node of current node (value already yeild to caller when IN
step).
sometimes, the node does not have right child. we should return false when caller ask hasNext()
.
the space here used in my solution is the stack
.
only tree nodes with left child will consume a space in the stack
.
because nodes without left child can be yield to caller directly.
Worst case
1
/
2
/
3
here we have to save node 1
and node 2
before we found node 3
.
but the space used in stack will be no more than 3.