Skip to content

Instantly share code, notes, and snippets.

@revanthpobala
Created February 21, 2017 02:30
Show Gist options
  • Save revanthpobala/757cf3cfc7520abae6cffb5945453f77 to your computer and use it in GitHub Desktop.
Save revanthpobala/757cf3cfc7520abae6cffb5945453f77 to your computer and use it in GitHub Desktop.
def morris_tree_traversal(root):
current = root
result = []
while current is not None:
if current.left is None:
result.append(current.val)
current = current.right
else:
pre = current.left
while(pre.right is not None and pre.right is not current):
pre = pre.right
if pre.right is None:
pre.right = current
current = current.left
else:
pre.right = None
result.append(current.val)
current = current.right
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment