Morris Preorder Traversal Achieves O(1) Space Without Stack or Recursion
Morris Preorder Traversal is an algorithm that performs binary tree preorder traversal without using a call stack or auxiliary stack, achieving O(1) extra space. It works by temporarily linking a node's inorder predecessor back to the current node, creating a structure known as a thread. Unlike the recursive or stack-based approaches that use O(H) space, this method traverses each edge at most twice, keeping time complexity at O(N). The key distinction from Morris Inorder Traversal is that the node is visited before the thread is created, rather than when the thread is removed. Once traversal of a subtree is complete, the temporary thread is deleted to restore the original tree structure.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in