What if you could explore a huge tree without carrying any extra memory baggage?
Why Morris Traversal Inorder Without Stack in DSA Python?
Imagine you want to visit every room in a big house, but you have no map and no way to remember which rooms you already visited except by writing notes on paper. Carrying and managing all those notes is tiring and easy to lose track.
Using a stack or recursion to remember which rooms (nodes) to visit next means extra memory and complexity. It can slow you down and make your plan bulky, especially if the house is huge. Also, managing this extra memory can cause mistakes and confusion.
Morris Traversal lets you visit every room in the house without any extra notes or memory. It cleverly uses the house's own doors (tree links) to remember where to go next, so you never get lost and don't need extra space.
def inorder_traversal(root): stack = [] current = root while current or stack: while current: stack.append(current) current = current.left current = stack.pop() print(current.val) current = current.right
def morris_inorder_traversal(root): current = root while current: if not current.left: print(current.val) current = current.right else: predecessor = current.left while predecessor.right and predecessor.right != current: predecessor = predecessor.right if not predecessor.right: predecessor.right = current current = current.left else: predecessor.right = None print(current.val) current = current.right
You can traverse a binary tree in order using only the tree itself, saving memory and making your code faster and cleaner.
When working with very large data trees in limited-memory devices like embedded systems or IoT gadgets, Morris Traversal helps process data efficiently without extra memory.
Morris Traversal visits nodes without extra memory like stacks or recursion.
It temporarily changes tree links to remember paths, then restores them.
This method is efficient for memory-limited environments.
Practice
Solution
Step 1: Understand Morris Traversal space usage
Morris Traversal uses threaded binary trees to avoid recursion or stack, so it uses O(1) extra space.Step 2: Compare with other methods
Traditional inorder traversal uses recursion or stack which requires O(h) space, where h is tree height.Final Answer:
It uses no extra space for stack or recursion. -> Option AQuick Check:
Space complexity = O(1) [OK]
- Thinking Morris is faster in time complexity
- Believing it only works on BSTs
- Assuming it permanently changes the tree
Solution
Step 1: Identify starting point for traversal
Morris traversal starts from the root node to cover the entire tree.Step 2: Check initialization options
Setting current = root ensures traversal begins at the tree's root.Final Answer:
current = root -> Option AQuick Check:
Start at root node [OK]
- Starting from None causes no traversal
- Starting from root.left misses root node
- Starting from root.right misses left subtree
2
/ \
1 3
Assuming the traversal prints node values in order.Solution
Step 1: Understand inorder traversal order
Inorder traversal visits left subtree, root, then right subtree.Step 2: Apply Morris traversal order
For the tree, left is 1, root is 2, right is 3, so output is [1, 2, 3].Final Answer:
[1, 2, 3] -> Option DQuick Check:
Inorder = left-root-right = [1, 2, 3] [OK]
- Confusing preorder or postorder with inorder
- Misreading tree structure
- Assuming traversal prints in insertion order
current = root
while current:
if current.left is None:
print(current.val)
current = current.right
else:
pre = current.left
while pre.right and pre.right != current:
pre = pre.right
if pre.right is None:
pre.right = current
current = current.left
else:
pre.right = None
print(current.val)
current = current.right
What is the main bug if the line pre.right = None is missing?Solution
Step 1: Understand role of
This line removes the temporary thread created to return to the root after left subtree traversal.pre.right = NoneStep 2: Consequence of missing this line
If not removed, the thread remains, causing the loop to revisit nodes endlessly, creating an infinite loop.Final Answer:
The traversal will enter an infinite loop. -> Option CQuick Check:
Missing thread removal = infinite loop [OK]
- Thinking it causes syntax error
- Assuming it changes traversal order
- Believing it skips nodes
result = []
current = root
while current:
if current.left is None:
# Change here
current = current.right
else:
pre = current.left
while pre.right and pre.right != current:
pre = pre.right
if pre.right is None:
pre.right = current
current = current.left
else:
pre.right = None
# Change here
current = current.right
Solution
Step 1: Identify where nodes are visited
In Morris traversal, nodes are visited and printed in two places: when left is None and after removing thread.Step 2: Replace print with appending to list
To collect values, replace both print statements withresult.append(current.val)to capture all visited nodes.Final Answer:
Replace both '# Change here' lines with result.append(current.val) -> Option BQuick Check:
Collect all visited nodes in list [OK]
- Appending only once misses some nodes
- Leaving print statements causes unwanted output
- Changing traversal logic breaks correctness
Ready to prove it?
Pass the test and this pattern moves unknown
