114. Flatten Binary Tree to Linked List¶
Edited by Charles
Medium · Python · Binary Tree
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Given the root of a binary tree, flatten the tree into a linked list in-place.
Original study notes follow, including intermediate snippets and earlier attempts.
Problem¶
Given the root of a binary tree, flatten the tree into a linked list in-place.
The flattened tree should satisfy:
The order of nodes should follow preorder traversal:
Example¶
Given:
After flattening:
Equivalent to:
The main idea is:
First recursively flatten the left subtree and the right subtree, then connect them together.
For every node:
we want to transform it into:
So the processing order is:
This is a postorder-style recursion.
For the current node:
After these two recursive calls, we can assume that both subtrees have already been flattened.
For example:
After recursively flattening the left subtree:
After recursively flattening the right subtree:
Now we only need to connect these two flattened lists.
Before changing the pointers, save them:
We need to do this because we are about to modify:
If we do not save the original right subtree, we may lose it.
The flattened tree must have:
So:
Then move the flattened left subtree to the right:
For example:
At this moment, the original right subtree 5 is temporarily disconnected, but we already saved it in:
Now we need to attach the original right subtree after the flattened left subtree.
Suppose the left subtree has already become:
We need to find the last node:
So:
After the loop, cur points to the last node of the current right chain.
Now connect:
For example:
After:
This gives the correct preorder order:
Preorder traversal is:
After flattening, we want:
So the right pointer chain becomes exactly:
which is preorder order.
Consider:
Process Node 2¶
Its left subtree is:
Its right subtree is:
After flattening both:
and:
Move left to right:
Then attach the original right subtree:
So the subtree rooted at 2 is finished.
Process Node 5¶
Node 5 has only a right child:
So it is already in the correct form.
Process Node 1¶
Now the left subtree is:
and the right subtree is:
Move the left subtree to the right:
Then attach the original right subtree:
Done.
For every node:
First:
Then:
Then:
Then find the end of the right chain:
Finally:
We write:
before changing the pointers.
Because after:
the original:
would be overwritten.
So we must save it first:
Otherwise, the original right subtree would be lost.
Time Complexity¶
The recursive traversal visits every node.
However, for every node, we may also search down the right chain:
In the worst case, this repeated search can make the total time complexity:
For example, when the tree is highly unbalanced.
Space Complexity¶
The recursion stack depends on the height of the tree:
For a balanced tree:
For a completely skewed tree:
The recursive idea is:
1. Flatten left subtree
2. Flatten right subtree
3. Move left subtree to the right
4. Find the end of the flattened left subtree
5. Attach the original right subtree
The structure changes from:
to:
The most important thing to remember is:
So after recursively flattening both subtrees, we simply connect them as:
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | September 10, 2026 |
Python solution¶
class Solution:
def flatten(self, root: Optional[TreeNode]) -> None:
if not root:
return
self.flatten(root.left)
self.flatten(root.right)
left = root.left
right = root.right
root.left = None
root.right = left
cur = root
while cur.right:
cur = cur.right
cur.right = right