Created
December 26, 2014 06:27
-
-
Save rohith2506/dfd54536d2667e28fe88 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
Amazing algorithm:- | |
inorder traversal without using stacks and recursion just by manipulating threads between them. | |
http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/ | |
*/ | |
void inorder_morris(node *root){ | |
if(root == NULL) return ; | |
node *cur, *pre; | |
cur = root; | |
while(cur != NULL){ | |
if(cur -> left == NULL){ | |
printf("%d", cur->data); | |
cur = cur -> right; | |
} | |
else{ | |
node *pre = cur -> left; | |
while(pre -> right != NULL && pre ->right != cur) | |
pre = pre -> right; | |
if(pre -> right == NULL){ | |
pre -> right = cur; | |
cur = cur -> left; | |
} | |
else{ | |
pre -> right = NULL; | |
printf("%d", cur -> data); | |
cur = cur -> right; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment