Watch 10 video solutions for Flatten Binary Tree to Linked List, a medium level problem involving Linked List, Stack, Tree. This walkthrough by take U forward has 305,660 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given the root of a binary tree, flatten the tree into a "linked list":
TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
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:
[0, 2000].-100 <= Node.val <= 100Follow up: Can you flatten the tree in-place (with
O(1) extra space)?Problem Overview: You receive the root of a binary tree and must transform it into a linked list in-place. The linked list should follow the same order as a preorder traversal (root → left → right). Every node’s left pointer becomes null, and the right pointer acts as the next pointer in the list.
Approach 1: Recursive Pre-order Traversal (O(n) time, O(h) space)
This approach uses a classic Depth-First Search recursion that processes nodes in preorder. Maintain a reference to the previously visited node. During traversal, set prev.right = current and prev.left = null to build the linked structure incrementally. The key insight is to traverse the right subtree after the left while rewiring pointers as you go. Time complexity is O(n) because each node is visited once. Space complexity is O(h), where h is the tree height due to the recursion stack.
Approach 2: Iterative Pre-order Traversal with Stack (O(n) time, O(n) space)
This method simulates preorder traversal using an explicit stack. Push the root node first. For each popped node, push its right child and then its left child so the left subtree is processed next. Rewire pointers by setting the current node’s right pointer to the next node on the stack and clearing left. The stack preserves preorder ordering without recursion. Time complexity remains O(n) since each node is pushed and popped once. Space complexity becomes O(n) in the worst case for skewed trees.
Both methods restructure the Binary Tree in-place without creating new nodes. The recursive approach is compact and mirrors the definition of preorder traversal. The iterative stack approach avoids recursion depth limits and provides more explicit control over traversal order.
Recommended for interviews: The recursive preorder DFS is usually the expected explanation because it clearly demonstrates pointer manipulation during traversal. Interviewers want to see that you understand preorder processing and in-place tree modification. The iterative stack version is a strong alternative when discussing recursion limits or implementing traversal without the call stack.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Pre-order Traversal | O(n) | O(h) | Preferred in interviews; concise DFS logic and natural preorder traversal |
| Iterative Pre-order Traversal (Stack) | O(n) | O(n) | When avoiding recursion or handling deep trees where recursion depth could overflow |