Binary Tree to DLL
A hard BST problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- BST
- Sheets
- 2
- Core for
- 2 roles
- Platform
- GeeksforGeeks
The problem
Given the root of a binary tree, convert it to a doubly linked list in-place. The conversion should be done in-place using the original tree nodes, where the left pointer acts as the previous pointer and the right pointer acts as the next pointer in the DLL. The order should follow inorder traversal.
Example 1
- Input
- [4,2,5,1,3]
- Output
- DLL: 1<->2<->3<->4<->5
- Why
- Inorder traversal of the tree is 1,2,3,4,5, which becomes the doubly linked list order.
Example 2
- Input
- [1]
- Output
- DLL: 1
- Why
- A single node becomes a DLL of length 1.
Example 3
- Input
- [1,2]
- Output
- DLL: 2<->1
- Why
- Inorder of tree [1,2] (where 2 is left child) is 2,1. The DLL follows this order.
Constraints
- The number of nodes is in the range [0, 1000].
- -1000 <= Node.val <= 1000
How to think about it
Updated 2026-09-09Converting a tree to a doubly linked list in inorder sequence does not require allocating new nodes. An inorder traversal visits nodes one by one; maintaining a pointer to the previously visited node lets you re-wire `prev.right = curr` and `curr.left = prev` on the fly, repurposing existing tree pointers directly.
Approaches, worst first
Inorder list buffer and pointer rewire
time O(n) · space O(n)
Traverse the tree to store all nodes in a flat list in inorder order, then iterate over the list updating each node's left and right pointers. Simple to implement, but uses O(n) auxiliary list memory.
In-place recursive inorder stitchingWrite this one
time O(n) · space O(h)
Perform standard inorder traversal with a persistent `prev` pointer. At each step, link `prev.right = curr` and `curr.left = prev`, then advance `prev = curr`. Memory is restricted purely to the recursion call stack.
Where people lose marks · 2
- Handling the head pointer: the head of the DLL is the very first node visited (leftmost leaf), which must be captured when `prev` is still null.
- Empty tree input: if `root == null`, returning null immediately prevents null pointer exceptions when attempting to access tree nodes.
The theory behind it
BST — the ground this problem stands on. All BST problems
What BST is
A binary search tree is a binary tree that enforces a strict ordering rule at every node. Every value stored in a node's left branch must be smaller than the node itself, and every value in its right branch must be larger. Because this rule holds true everywhere down the tree, searching for a value does not require checking every branch. At each step, a single comparison lets you discard an entire half of the remaining nodes.
When to reach for it
Reach for a binary search tree when problems mention sorted tree structures, finding the kth smallest element, checking tree validity, or searching within a range of values. Signals include queries for the next greater element in a dynamic set, finding the lowest common ancestor in a sorted tree, or converting sorted arrays into height-balanced trees. When data must remain sorted while supporting continuous insertions and lookups, BST properties are the target.
How the pattern works
Use the ordering property to prune entire subtrees. When looking for a key, move left if the target is smaller than the current node, or move right if it is larger, stopping when values match or when reaching a null link. For validating a tree, do not merely compare a node to its immediate children; pass valid lower and upper value bounds down through recursive calls. Walking a valid binary search tree in-order visits all values in strictly increasing numerical order.
What each operation costs
| Operation | Time |
|---|---|
| search, insert, or delete on balanced tree | O(log n) |
| search, insert, or delete on degenerate line tree | O(n) |
| in-order traversal visiting all nodes in sorted order | O(n) |
What usually goes wrong with BST
- Validating a tree by checking only immediate children against the parent, missing violations where a node deep in the left subtree is larger than an ancestor higher up.
- Allowing duplicate values on the wrong side when the problem specification requires strictly smaller values on the left and strictly greater on the right.
- Deleting a node with two children by removing it directly instead of replacing its value with its in-order predecessor or successor and deleting that leaf instead.
Which roles need this problem
BST is a core topic for these 2 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 6 more roles, including Full-Stack Developer, Android Developer, iOS Developer.
Companies that have asked it
Tags taken from the problem's own GeeksforGeeks page — not a copied list.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore BST problems
Problem set and role mapping as of .