Search topics...
All Problems

Flatten Binary Tree to Linked List

Solution Approach
Was this helpful?
Mediumlinked-listExpected: O(n) time, O(1) space
stacklinked-listrecursion

Problem

Given the root of a binary tree, flatten the tree into a linked list in-place following pre-order traversal.

Example 1:

Input:     1
          / \
         2   5
        / \   \
       3   4   6

Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6
(Each node's left is null, right points to next in pre-order)

Example 2:

Input: root = []
Output: []
Reference solution unlocks after your first submission
Loading...