Created
June 10, 2013 04:14
-
-
Save bittib/5746501 to your computer and use it in GitHub Desktop.
Morris InOrder Traverse Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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