Flatten Binary Tree to Linked List
In-place pointer rewiring into preorder right chain
Python Code
4 5 while curr:● if curr.left:7 pred = curr.left......9 pred = pred.right10 pred.right = curr.right11 curr.right = curr.left12 curr.left = None13 curr = curr.rightCurrent Line (6): Check left subtree
Tree Structure
Operation:Visit Node
Flatten Progress
Flatten Stack
Stack is empty. Click Next to begin!
Step Explanation
Line 6: pick_root
Visit node 1 in preorder chain
- > Meaning: Only nodes with left child need rewiring.
- > Why: If left is absent, structure already respects linked-list form locally.
- > Next: Find predecessor in left subtree.
- > Progress snapshot: 0/21 steps, chain size 0.
Unvisited
Left
Current
Right
Done