Skip to content

Flatten Binary Tree to Linked List

Flatten a binary tree into a “linked list” where the linked list is represented using the right child pointer of each node and the left child pointer is always null.

InputOutputOrder
[1,2,5,3,4,null,6][1,null,2,null,3,null,4,null,5,null,6]Pre-order: 1→2→3→4→5→6
  • The number of nodes is in the range [0, 2000].
  • -100 <= Node.val <= 100
ApproachTimeSpaceBest When
Pre-Order DFSO(n)O(h)Intuitive; finds rightmost before attaching
Post-Order DFSO(n)O(h)Elegant reverse traversal; track previous
★ Recommended

Recursively flatten left and right subtrees, then find the rightmost node of the flattened left subtree and attach the right subtree to it.

⏱ Time O(n) Visit each node once 💾 Space O(h) Recursion depth
function flatten(root):
if not root: return
flatten(root.left)
flatten(root.right)
if root.left:
rightmost = root.left
while rightmost.right:
rightmost = rightmost.right
rightmost.right = root.right
root.right = root.left
root.left = null
flatten_binary_tree_to_linked_list_preorder_dfs.py
from typing import Optional
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def flatten(root: Optional[TreeNode]) -> None:
"""
Flatten binary tree to linked list using pre-order DFS.
Recursively flattens left and right subtrees, then rewires pointers.
Time: O(n), Space: O(h) for recursion stack
"""
if not root:
return
flatten(root.left)
flatten(root.right)
if root.left:
# Find the rightmost node in flattened left subtree
rightmost = root.left
while rightmost.right:
rightmost = rightmost.right
# Attach right subtree to rightmost node
rightmost.right = root.right
# Move flattened left subtree to right
root.right = root.left
root.left = None
# Test cases
if __name__ == "__main__":
# Example 1: [1,2,5,3,4,null,6]
root = TreeNode(1)
root.left = TreeNode(2)
root.left.left = TreeNode(3)
root.left.right = TreeNode(4)
root.right = TreeNode(5)
root.right.right = TreeNode(6)
flatten(root)
current = root
result = []
while current:
result.append(current.val)
current = current.right
print(f"Example 1: {result}") # [1, 2, 3, 4, 5, 6]
# Example 2: Single node
single = TreeNode(0)
flatten(single)
print("Example 2: Single node tree flattened")

Approach 2: Post-Order DFS with Previous Tracking

Section titled “Approach 2: Post-Order DFS with Previous Tracking”
optimal

Traverse in post-order (right → left → node), tracking the previous node. Attach previous to current’s right and nullify left.

⏱ Time O(n) Single pass 💾 Space O(h) Recursion depth
function flatten(root):
prev = null
function dfs(node):
if not node: return
dfs(node.right)
dfs(node.left)
node.right = prev
node.left = null
prev = node
dfs(root)
flatten_binary_tree_to_linked_list_postorder_dfs.py
from typing import Optional
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def flatten(root: Optional[TreeNode]) -> None:
"""
Flatten binary tree to linked list using post-order DFS with previous tracking.
Uses a list to track the previous node in-order.
Time: O(n), Space: O(h) for recursion stack
"""
prev = [None]
def dfs(node):
if not node:
return
# Post-order: right, left, then process node
dfs(node.right)
dfs(node.left)
node.right = prev[0]
node.left = None
prev[0] = node
dfs(root)
# Test cases
if __name__ == "__main__":
# Example 1: [1,2,5,3,4,null,6]
root = TreeNode(1)
root.left = TreeNode(2)
root.left.left = TreeNode(3)
root.left.right = TreeNode(4)
root.right = TreeNode(5)
root.right.right = TreeNode(6)
flatten(root)
current = root
result = []
while current:
result.append(current.val)
current = current.right
print(f"Example 1: {result}") # [1, 2, 3, 4, 5, 6]
# Example 2: Single node
single = TreeNode(0)
flatten(single)
print("Example 2: Single node tree flattened")