0
0
DSA Goprogramming~10 mins

Morris Traversal Inorder Without Stack in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - Morris Traversal Inorder Without Stack
Start at root
Check if current.left is nil?
YesVisit current node
Move to current.right
Find predecessor in left subtree
Check if predecessor.right is nil?
YesMake thread: predecessor.right = current
Remove thread: predecessor.right = nil
Move current to current.left
Move current to current.right
Repeat
This flow shows how Morris Traversal visits nodes without stack by creating temporary threads to predecessors and removing them after visiting.
Execution Sample
DSA Go
func morrisInorder(root *Node) {
  current := root
  for current != nil {
    if current.left == nil {
      visit(current)
      current = current.right
    } else {
      predecessor := current.left
      for predecessor.right != nil && predecessor.right != current {
        predecessor = predecessor.right
      }
      if predecessor.right == nil {
        predecessor.right = current
        current = current.left
      } else {
        predecessor.right = nil
        visit(current)
        current = current.right
      }
    }
  }
}
This code performs inorder traversal of a binary tree without using stack or recursion by creating temporary threads.
Execution Table
StepOperationCurrent NodePredecessorThread Created/RemovedVisual State
1Start at root1nilnil1 / \ 2 3
2current.left != nil, find predecessor12nil1 / \ 2 3
3predecessor.right == nil, create thread12Thread 2.right -> 11 / \ 2->1 3
4Move current to left child2nilnil1 / \ 2->1 3
5current.left == nil, visit node2nilnilVisited: 2 1 / \ 2->1 3
6Move current to right child (thread)12nil1 / \ 2->1 3
7predecessor.right == current, remove thread12Remove thread 2.right1 / \ 2 3
8Visit current node1nilnilVisited: 2,1 1 / \ 2 3
9Move current to right child3nilnil1 / \ 2 3
10current.left == nil, visit node3nilnilVisited: 2,1,3 1 / \ 2 3
11Move current to right child (nil)nilnilnilTraversal complete
12ExitnilnilnilTraversal ends as current is nil
💡 Traversal ends when current becomes nil, meaning all nodes visited.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6After Step 7After Step 9Final
current112113nil
predecessornil2nil22nilnil
threadnilCreated at 2.rightCreated at 2.rightCreated at 2.rightRemoved at 2.rightnilnil
visited nodes[][][][2][2][2,1][2,1,3]
Key Moments - 3 Insights
Why do we create a thread from predecessor.right to current?
Creating a thread allows us to return to the current node after finishing the left subtree without using a stack, as shown in steps 3 and 6.
Why do we remove the thread after visiting the current node?
Removing the thread restores the original tree structure and prevents infinite loops, as seen in step 7 where the thread is removed before visiting the node.
What happens when current.left is nil?
When current.left is nil, we visit the current node immediately and move to current.right, as in steps 5 and 10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'current' after step 4?
A3
B1
C2
Dnil
💡 Hint
Check the 'current' column in execution_table row for step 4.
At which step is the thread from predecessor.right to current removed?
AStep 7
BStep 3
CStep 6
DStep 9
💡 Hint
Look at the 'Thread Created/Removed' column in execution_table.
If the tree had no left children, how would the traversal proceed?
AIt would create threads for each node
BIt would visit nodes and move right immediately without threads
CIt would get stuck in an infinite loop
DIt would skip visiting nodes
💡 Hint
Refer to steps where current.left == nil in execution_table and variable_tracker.
Concept Snapshot
Morris Traversal Inorder Without Stack:
- Uses threaded binary tree to avoid stack/recursion
- Creates temporary threads from predecessor.right to current
- Visits nodes when left subtree is done or no left child
- Removes threads to restore tree
- Time: O(n), Space: O(1)
Full Transcript
Morris Traversal is a way to do inorder traversal of a binary tree without using extra space like stack or recursion. It works by creating temporary threads from the rightmost node of the left subtree (predecessor) back to the current node. This thread acts like a return path after visiting the left subtree. When the thread is encountered again, it is removed and the current node is visited. If the current node has no left child, it is visited immediately and traversal moves to the right child. This process continues until all nodes are visited and the current pointer becomes nil, ending the traversal. This method modifies the tree temporarily but restores it before finishing, achieving O(n) time and O(1) space complexity.