4 ms·
If the nodes have parent pointers, which does tend to be somewhat common, then depth-first can be done in constant space. I haven't implemented it before, but I
by userbinator 10y ago
If the nodes have parent pointers, which does tend to be somewhat common, then depth-first can be done in constant space. I haven't implemented it before, but I believe breadth-first should also take constant space in this case.
- catnaroek 10y agoIf the nodes have parent pointers, everything is paying extra O(n) space.
- perfmode 10y agoSpace is already O(n)
- deleted 10y ago[deleted]
- junke 10y agoRelated: Morris Traversal (no parent link required). https://en.wikipedia.org/wiki/Tree_traversal#Morris_in-order_traversal_using_threading https://en.wikipedia.org/wiki/Tree_traversal#Morris_in-order... Trying to reimplement it without looking at the solution is fun.