Step 1: Start at root (4), find left child (2), find rightmost node in left subtree (3)
4 -> [curr]
2
/ \
1 3 -> right points to null
Why: We need to find where to return after finishing left subtree
Step 2: Make 3's right point to 4 (thread), move curr to left child 2
4
/ \
2 -> [curr]
/ \
1 3 -> right -> 4 (thread)
Why: Thread helps us return to 4 after left subtree
Step 3: At 2, find left child (1), rightmost in left subtree is 1 (no right child)
4
/ \
2 -> [curr]
/ \
1 -> right -> null
3 -> right -> 4 (thread)
Why: Find thread point to return after left subtree of 2
Step 4: Make 1's right point to 2 (thread), move curr to left child 1
4
/ \
2
/ \
1 -> [curr], right -> 2 (thread)
3 -> right -> 4 (thread)
Why: Thread helps us return to 2 after left subtree
Step 5: At 1, no left child, print 1, move curr to right (thread to 2)
1 -> printed
curr -> 2 (via thread)
Why: Left subtree done, visit node and move to right
Step 6: At 2, thread from 1 detected, remove thread, print 2, move curr to right child 3
2 -> printed
1's right reset to null
curr -> 3
Why: Restore tree and visit node after left subtree
Step 7: At 3, no left child, print 3, move curr to right (thread to 4)
3 -> printed
curr -> 4 (via thread)
Why: Visit node and move to right
Step 8: At 4, thread from 3 detected, remove thread, print 4, move curr to right child 5
4 -> printed
3's right reset to null
curr -> 5
Why: Restore tree and visit node after left subtree
Step 9: At 5, no left child, print 5, move curr to right null (end)
5 -> printed
curr -> null (end)
Result: 1 -> 2 -> 3 -> 4 -> 5 -> null