If the tree is connected in both directions, you can do this:
assert root.parent is null now, old = root, null while now != null: print now.label if leaf(now): now, old = now.parent, now else: if old == now.right: now, old = now.parent, now if old == now.left: now, old = now.right, now if old == now.parent: now, old = now.left, now
It prints in root, left, right order, but you can get any order you like.
I do not think you can do this if the tree is connected in only one direction. You can watch Deforestation .
source share