The pure recursive function in fact does the same: the right-most of the left subtree connected to x and x connected to the left-most of the right subtree. I thought you might be interested to see the congruity.
public DLL dll(Node x) {
return dll(null, x, null);
}
public DLL dll(DLL before, Node x, DDL after) {
if (x == null) {
return;
}
if (x.left != null) {
before = dll(before, x.left, null);
}
if (x.right != null) {
after = dll(null, x.left, after);
}
DLL result = new DLL();
result.insert(x.value);
result.insertBefore(before); // null being a no-op.
result.insertAfter(after); // null being a no-op.
return result;
}
As one can see, there are several variations thinkable.