Skip to content

Flatten Binary Tree to Linked List

Given the root of a binary tree, flatten the tree into a “linked list”:

  • The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
  • The “linked list” should be in the same order as a pre-order traversal of the binary tree.

Example 1:

Example 1

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

Example 2:

Input: root = []
Output: []

Example 3:

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

Constraints:

  • The number of nodes in the tree is in the range [0, 2000].
  • −100≤-100 \le Node.val ≤100\le 100

Follow-up: Can you flatten the tree in-place (with O(1) extra space)?

Pre-order visits a node, then its whole left subtree, then its right subtree. Inside the left subtree, the last node in pre-order is its rightmost node (keep going right until there is no right child). So for a node curr with a left child, the right subtree must come right after that rightmost node.

That gives a local rewiring that keeps pre-order intact: hang curr->right off the rightmost node of the left subtree, then move the left subtree into curr->right and clear curr->left. Now curr has no left child, and the pre-order of the tree is unchanged. Repeating this at every node while walking down the right pointers flattens the tree using only a couple of pointers, which answers the follow-up (the same idea as a Morris traversal). A recursive or stack-based DFS would need O(h)O(h) extra space instead.

  1. Set curr = root.
  2. While curr is not nullptr:
    • If curr->left exists, find its rightmost node pre by following right pointers from curr->left.
    • Set pre->right = curr->right, then curr->right = curr->left and curr->left = nullptr.
    • Move to curr = curr->right.
  3. Every node now has left == nullptr, and the right pointers follow pre-order.

Example 1. At curr = 1, the left subtree is rooted at 2 and its rightmost node is 4. The right subtree 5 → 6 is hung off 4, and the subtree at 2 moves to 1’s right (dashed edges are the new links):

Example 1 at curr = 1: new right link from 1 to 2; 2 keeps left child 3 and right child 4, which is pre; new right link from 4 to 5; 5 links right to 6

Next curr = 2: its left child 3 has no right child, so pre = 3; 3->right = 4, 2->right = 3. The remaining nodes have no left children, and the list reads 1 → 2 → 3 → 4 → 5 → 6.

  • Time: O(n)O(n) — curr visits each node once. A search for pre walks right-edges on the right spine of curr’s left subtree. After the splice, every node on that spine has all its ancestors reaching it through right-edges, so it is never again inside some node’s left subtree and its edge is never walked by a later search. Hence all searches together walk each edge at most once.
  • Space: O(1)O(1) — only curr and pre; no recursion and no new nodes.
  • Empty tree (Example 2): curr starts as nullptr, so the loop does nothing.
  • Single node (Example 3) or an already flat tree with only right children: no node has a left child, so curr simply walks the chain.
  • Only left children (a left-leaning path): at each node pre is the single left child, and the chain is turned to the right one node at a time.

Think before you peek

Work out an approach on your own first — the explanation will still be here when you want it.

class Solution {
public:
void flatten(TreeNode* root) {
for (TreeNode* curr = root; curr; curr = curr->right) {
if (!curr->left) continue;
TreeNode* pre = curr->left; // last node of the left subtree in pre-order
while (pre->right) pre = pre->right;
pre->right = curr->right;
curr->right = curr->left;
curr->left = nullptr;
}
}
};
class Solution {
public void flatten(TreeNode root) {
for (TreeNode curr = root; curr != null; curr = curr.right) {
if (curr.left == null) continue;
TreeNode pre = curr.left; // last node of the left subtree in pre-order
while (pre.right != null) pre = pre.right;
pre.right = curr.right;
curr.right = curr.left;
curr.left = null;
}
}
}
class Solution:
def flatten(self, root: TreeNode | None) -> None:
"""
Do not return anything, modify root in-place instead.
"""
curr = root
while curr:
if curr.left:
pre = curr.left # last node of the left subtree in pre-order
while pre.right:
pre = pre.right
pre.right = curr.right
curr.right = curr.left
curr.left = None
curr = curr.right

Give it a try first

Attempt the problem on LeetCode — the code will still be here.