2Brute2Best
πŸ”₯0

Flatten Binary Tree to Linked List

MediumΒ·LeetCode #114 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given the root of a binary tree. Change the tree in place so it becomes a flat chain made of the same nodes.

Use right to link each node to the next one, and set every left to null. The chain must follow the preorder of the original tree: the node first, then its whole left subtree, then its whole right subtree.

Input

The root of a binary tree. The tree can be empty.

Output

Nothing is returned. The tree itself is changed into the flat chain.

Example 1
Input: root = [1, 2, 5, 3, 4, null, 6]
Output: [1, null, 2, null, 3, null, 4, null, 5, null, 6]

The preorder is 1, 2, 3, 4, 5, 6. Each right points to the next value, and every left is null.

Example 2
Input: root = []
Output: []

There is nothing to flatten.

Example 3
Input: root = [0]
Output: [0]

A single node is already a flat chain.

Constraints
  • β€’ 0 ≀ number of nodes ≀ 2000
  • β€’ βˆ’100 ≀ Node.val ≀ 100
  • β€’ Follow-up: can you flatten the tree in place using O(1) extra space?
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œin place”
β†’ Only pointers change. No new nodes and no extra list of nodes.
β€œpreorder”
β†’ The chain order is preorder, so the left subtree has to sit between a node and its right subtree.
β€œleft must be null”
β†’ Every left pointer has to be cleared, so the left subtree is moved over, not copied.
03

Approach Complexity Ladder

Brute Force β€” save preorder in a list, then relinkO(N)O(N)

Collect the nodes in preorder into a vector, then point each right at the next one

Better β€” reverse preorder with a prev pointerO(N)O(H)

Walk right, then left, then the node, and point each right at the node built before it

Optimal β€” move each left subtree, O(1) spaceO(N)O(1)

Walk down the chain; for each left subtree, hang the right subtree at its rightmost node and move it right

Brute Force Intuition

Walk the tree in preorder and store every node pointer in a vector. Then go down the vector: set each node's left to null and its right to the next node in the vector. Nothing is relinked during the walk, so the old links are still there when we need them.

Recognition rule for this sub-pattern:

Rebuild or reshape a tree; decide the order you visit nodes so pointers are not lost.
Remember this Β· Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.