Preorder morris traversal 07/29
void morrisTraversalPreorder(Node node) {
while (node != null) {
// If left child is null, print the current node data. Move to
// right child.
if (node.left == null) {
System.out.print(node.data + " ");
node = node.right;
} else {
// Find inorder predecessor
Node current = node.left;
while (current.right != null && current.right != node) {
current = current.right;
}
// If the right child of inorder predecessor already points to
// this node
if (current.right == node) {
current.right = null;
node = node.right;
}
// If right child doesn't point to this node, then print this
// node and make right child point to this node
else {
System.out.print(node.data + " ");
current.right = node;
node = node.left;
}
}
}
}
Last updated