Flatten Binary Tree to Linked List

Medium Trees & BST Tree to Lists and vice-versa Original on LeetCode

Flatten Binary Tree to Linked List is a medium trees & bst problem solved with the tree to lists and vice-versa pattern. The best approach, optimal (morris-style rewiring, o(1) space), runs in O(n) time and O(1) space. Below are 2 approaches in Java, from preorder list, then relink up.

Problem

Flatten a binary tree in place into a “linked list” that uses the right pointers, in preorder order. Every left pointer must become null.

Examples

Example 1

Input
root = [1, 2, 5, 3, 4, null, 6]
Output
[1, null, 2, null, 3, null, 4, null, 5, null, 6]
Why
Preorder order, linked through right pointers.

Example 2

Input
root = []
Output
[]

Constraints

  • The tree has 0 to 2000 nodes.
  • In place; every left pointer must end up null.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.

ApproachTimeSpace
Preorder list, then relinkO(n)O(n)
Optimal (Morris-style rewiring, O(1) space)O(n)O(1)

2Optimal (Morris-style rewiring, O(1) space)

TimeO(n)
SpaceO(1)

Walk down the right spine. At a node with a left child, find the rightmost node of the left subtree, hang the node's current right subtree off it, move the left subtree to the right and clear left. Continue to the right.

  1. cur = root.
  2. If cur.left != null: pred = rightmost(cur.left); pred.right = cur.right; cur.right = cur.left; cur.left = null.
  3. cur = cur.right.
Java
class Solution {
    public void flatten(TreeNode root) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.left != null) {
                TreeNode pred = cur.left;
                while (pred.right != null) pred = pred.right;
                pred.right = cur.right;
                cur.right = cur.left;
                cur.left = null;
            }
            cur = cur.right;
        }
    }
}

Edge cases to test

  • Empty tree
  • Only left children

Hints

Hint 1

For each node with a left child, the rightmost node of that left subtree should point to the node's right subtree.

FAQ

What is the best time complexity for Flatten Binary Tree to Linked List?

Optimal (Morris-style rewiring, O(1) space) runs in O(n) time and O(1) extra space.

Which pattern does Flatten Binary Tree to Linked List use?

It is a trees & bst problem that uses the tree to lists and vice-versa pattern. Other problems with the same pattern: Binary Tree to DLL.

Is there a brute force solution for Flatten Binary Tree to Linked List?

Yes. Preorder list, then relink takes O(n) time and O(n) space. Collect nodes in preorder, then set each node's left to null and right to the next node.

Which edge cases should I test for Flatten Binary Tree to Linked List?

Empty tree; Only left children.