Skip to content

Instantly share code, notes, and snippets.

@bittib
Created June 10, 2013 04:14
Show Gist options
  • Save bittib/5746501 to your computer and use it in GitHub Desktop.
Save bittib/5746501 to your computer and use it in GitHub Desktop.
Morris InOrder Traverse Algorithm
public static void morrisInOrderTraverse(TreeNode root){
TreeNode ptr = root;
while (ptr != null){
if (ptr.left == null){
visit(ptr);
ptr = ptr.right;
}else{
TreeNode node = ptr.left;
while (node.right != null && node.right != ptr)
node = node.right;
if (node.right == null){
node.right = ptr;
ptr = ptr.left;
}else{
visit(ptr);
node.right = null;
ptr = ptr.right;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment