Binary Tree to DLL
Binary Tree to DLL is a hard trees & bst problem solved with the tree to lists and vice-versa pattern.
The best approach, optimal (inorder with a prev pointer), runs in O(n) time and O(h) space.
Below are 2 approaches in Java, from collect nodes, then link up.
Problem
Convert a binary tree in place into a doubly linked list that follows the tree’s inorder order. Reuse each node’s left pointer as prev and its right pointer as next, and return the head of the list. (GFG names the class Node; here we use TreeNode.)
Examples
Example 1
- Input
root = [10, 12, 15, 25, 30, 36]- Output
25 <-> 12 <-> 30 <-> 10 <-> 36 <-> 15- Why
- The list follows inorder order.
Example 2
- Input
root = [1, 3, 2]- Output
3 <-> 1 <-> 2
Constraints
- The tree has
1to10^5nodes. - Do it in place: left becomes prev and right becomes next.
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.
| Approach | Time | Space |
|---|---|---|
| Collect nodes, then link | O(n) | O(n) |
| Optimal (inorder with a prev pointer) | O(n) | O(h) |
1Collect nodes, then link
O(n)O(n)Store the nodes in inorder order in a list, then link neighbours.
- Inorder into a list.
- For i: list[i].left = list[i - 1]; list[i].right = list[i + 1].
class Solution {
TreeNode bToDLL(TreeNode root) {
List<TreeNode> nodes = new ArrayList<>();
inorder(root, nodes);
for (int i = 0; i < nodes.size(); i++) {
nodes.get(i).left = i > 0 ? nodes.get(i - 1) : null;
nodes.get(i).right = i + 1 < nodes.size() ? nodes.get(i + 1) : null;
}
return nodes.get(0);
}
private void inorder(TreeNode n, List<TreeNode> out) {
if (n == null) return;
inorder(n.left, out);
out.add(n);
inorder(n.right, out);
}
}2Optimal (inorder with a prev pointer)
O(n)O(h)Recursion only; the tree's own pointers become the list.Traverse inorder, keeping the previously visited node. Link it to the current one as you go. The first visited node is the head.
- go(left).
- If prev is null, head = cur; else prev.right = cur, cur.left = prev. prev = cur.
- go(right).
class Solution {
private TreeNode head, prev;
TreeNode bToDLL(TreeNode root) {
head = prev = null;
convert(root);
return head;
}
private void convert(TreeNode cur) {
if (cur == null) return;
convert(cur.left);
if (prev == null) head = cur;
else { prev.right = cur; cur.left = prev; }
prev = cur;
convert(cur.right);
}
}Edge cases to test
- Single node
- Skewed tree
Hints
Hint 1
Do an inorder traversal while remembering the previously visited node. Link prev.right = cur and cur.left = prev.
FAQ
What is the best time complexity for Binary Tree to DLL?
Optimal (inorder with a prev pointer) runs in O(n) time and O(h) extra space.
Which pattern does Binary Tree to DLL use?
It is a trees & bst problem that uses the tree to lists and vice-versa pattern. Other problems with the same pattern: Flatten Binary Tree to Linked List.
Is there a brute force solution for Binary Tree to DLL?
Yes. Collect nodes, then link takes O(n) time and O(n) space. Store the nodes in inorder order in a list, then link neighbours.
Which edge cases should I test for Binary Tree to DLL?
Single node; Skewed tree.