Skip to main content

Flatten Binary Tree to Linked List

LeetCode 114 | Difficulty: Medium​

Medium

Problem Description​

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**](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR) of the binary tree.

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 <= Node.val <= 100`

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

Topics: Linked List, Stack, Tree, Depth-First Search, Binary Tree


Approach​

Tree DFS​

Traverse the tree recursively (or with a stack). At each node, decide: what information do I need from the left/right subtrees? Process: go left β†’ go right β†’ combine results. Consider preorder, inorder, or postorder traversal based on when you need to process the node.

When to use

Path problems, subtree properties, tree structure manipulation.

Stack​

Use a stack (LIFO) to track elements that need future processing. Process elements when a "trigger" condition is met (e.g., finding a smaller/larger element). Monotonic stack maintains elements in sorted order for next greater/smaller element problems.

When to use

Matching brackets, next greater element, evaluating expressions, backtracking history.

Linked List​

Use pointer manipulation. Common techniques: dummy head node to simplify edge cases, fast/slow pointers for cycle detection and middle finding, prev/curr/next pattern for reversal.

When to use

In-place list manipulation, cycle detection, merging lists, finding the k-th node.


Solutions​

Solution 1: C# (Best: 168 ms)​

MetricValue
Runtime168 ms
Memory36.6 MB
Date2022-01-13
Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public void Flatten(TreeNode root) {
TreeNode current = root;
while(current != null)
{
if(current.left != null) {
TreeNode prev = current.left;
while(prev.right != null)
{
prev = prev.right;
}
prev.right = current.right;
current.right = current.left;
current.left = null;
}
current = current.right;
}

}
}

Complexity Analysis​

ApproachTimeSpace
Tree Traversal$O(n)$$O(h)$
Stack$O(n)$$O(n)$
Linked List$O(n)$$O(1)$

Interview Tips​

Key Points
  • Discuss the brute force approach first, then optimize. Explain your thought process.
  • Consider: "What information do I need from each subtree?" β€” this defines your recursive return value.
  • Draw the pointer changes before coding. A dummy head node simplifies edge cases.
  • Think about what triggers a pop: is it finding a match, or finding a smaller/larger element?
  • LeetCode provides 1 hint(s) for this problem β€” try solving without them first.
πŸ’‘ Hints

Hint 1: If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.