Is it possible to do an iterative in-order-traversal on a BST whose node has a parent pointer (the parent of the root is null) without using a visited flag or a stack?
I googled and didn’t find a reply. The point is, how can I know – at a certain node – that I’ve just come to it vs I’ve finished everything underneath it?
You can do that, you just need to remember the last visited node along with the current node. Doing this is not disallowed by the problem statement: both
visitedflag on each node and astackare (worst case) O(n), remembering the last node is just O(1).In C#, the algorithm could look like this: