Flatten Binary Tree to Linked List
A medium Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Binary Trees
- Sheets
- 1
- Core for
- 4 roles
The problem
Rearrange a binary tree in place so it becomes a single chain that follows preorder: each node's right link points to the node that comes next in preorder, and every left link becomes empty. Reuse the existing nodes.
Example 1
- Input
- [5,2,9,1,3,8]
- Output
- [5,null,2,null,1,null,3,null,9,null,8]
- Why
- Preorder order 5,2,1,3,9,8, each node hung off the right pointer.
Example 2
- Input
- []
- Output
- []
- Why
- An empty tree remains empty.
Example 3
- Input
- [0]
- Output
- [0]
- Why
- A single node tree remains unchanged.
Constraints
- The number of nodes is in the range [0, 2000].
- -100 <= Node.val <= 200
How to think about it
Updated 2026-09-29In a preorder traversal, the right subtree of any node must follow the entire left subtree. That means the original right child should be attached directly to the rightmost node (predecessor) of the left subtree. Doing this restructuring iteratively rewires the tree without extra storage.
Approaches, worst first
Preorder array buffer
time O(n) · space O(n)
Perform preorder traversal and store all node references in a list. Iterate through the list re-pointing `node[i].left = null` and `node[i].right = node[i+1]`. Correct but requires linear auxiliary array space.
Reverse postorder recursion
time O(n) · space O(h)
Recurse right then left while holding a `prev` pointer. Point `curr.right = prev`, `curr.left = null`, and update `prev = curr`. Reverses the preorder construction cleanly through recursion.
Morris-style pointer graftingWrite this one
time O(n) · space O(1)
Iterate with `curr`. If `curr.left` exists, find its rightmost descendant, attach `curr.right` to that descendant's right, move `curr.left` to `curr.right`, and set `curr.left = null`. Advance `curr = curr.right`. Operates in place with no stack.
Where people lose marks · 3
- Forgetting to null out `curr.left` leaves behind invalid left-child pointers, violating the singly-linked list requirement.
- Overwriting `curr.right` before grafting the original right subtree to the left subtree's rightmost leaf permanently loses the right subtree.
- Failing to guard against null root causes immediate exceptions on empty tree inputs.
The theory behind it
Binary Trees — the ground this problem stands on. All Binary Trees problems
What Binary Trees is
A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.
When to reach for it
Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.
How the pattern works
Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.
More Binary Trees problems
Target Roles
Core requirement for 4 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .