loading

Flatten Binary Tree to Linked List — Step-by-Step Visualization

mediumLeetCode #114Linked ListStackTreeDFSBinary Tree

Flatten binary tree to in-place linked list in preorder.

Algorithm Pattern

In-Place Preorder Flattening

Key Idea

For each node with left child, attach right subtree to leftmost right node, then move left to right.

Step-by-Step Approach

  1. While node has left child
  2. Find rightmost node of left subtree
  3. Attach node.right to rightmost.right
  4. Move node.left to node.right
  5. Set node.left = null
  6. Advance to node.right

Related Problems