Scrivio Interview library
TreesTrees and binary search treesReported 17 times · 16 min read · updated 15 November 2026

Convert a binary tree into a doubly linked list in in-order sequence.

1Turning a tree of people into a line holding hands

Imagine a group of people standing in the shape of a family tree on a stage. Each person has two hands. In the tree, a person's left hand rests on the shoulder of their left child and their right hand on their right child. Now the director asks everyone to form a single line instead, in a particular order, with each person holding the hand of the person before them with the left hand, and the person after them with the right hand. Nobody new comes on stage, and nobody grows a third hand. The same two hands simply change what they hold.

That is the whole problem. A binary tree is made of nodes, each with a value and two pointers, left and right, to its children. A doubly linked list is a chain in which every node points to both its previous node and its next node, so it can be walked in both directions. We are asked to rearrange the tree's own nodes into such a chain, reusing left as the previous pointer and right as the next pointer.

The order of the line is in-order sequence. In-order means: first everything in a node's left subtree, then the node itself, then everything in its right subtree, applied again inside each subtree. A subtree is a node together with everything below it.

Our example has root 10. Its left child is 6, which has children 4 and 8, and its right child is 14, which has only a right child, 16. Let us list the in-order sequence by hand. To place 10, we first list its left subtree, the one rooted at 6. Inside it, 4 comes first, then 6, then 8. Then comes 10 itself. Then its right subtree: 14 has no left subtree, so 14, then 16. The sequence is 4, 6, 8, 10, 14, 16, and the list we must hand back is 4 <-> 6 <-> 8 <-> 10 <-> 14 <-> 16, starting at 4.

PictureThe example tree and the list we must make from it

The tree we are given. Its in-order sequence is the order the list must have.

  1. 106144816
    in-order
    4 6 8 10 14 16
    list
    4 <-> 6 <-> 8 <-> 10 <-> 14 <-> 16
    head
    4

    1In-order reads left subtree, node, right subtree, which gives 4, 6, 8, 10, 14, 16. The list must start at 4, the leftmost node.

What to assume

Must we reuse the tree's nodes, or may we build new list nodes?
We reuse them, in place.
Which pointer is previous and which is next?
Left is previous, right is next.
Should the list be circular, with the head and the tail joined?
Not in this version.
What do we return?
The head, the first node, or null for an empty tree. The tree may be any binary tree; if it happens to be a binary search tree, the list simply comes out sorted.

Notice something reassuring. We do not need to invent any order; the traversal we already know produces it. And each node needs only two changes, its left and its right, so the problem is smaller than it looks. The only real care is not to cut a pointer we still need.

A careful candidate asks a few questions. Must we reuse the tree's nodes, or may we build new list nodes? We reuse them, in place. Which pointer is previous and which is next? Left is previous, right is next. Should the list be circular, with the head and the tail joined? Not in this version; a follow-up shows the change. What do we return? The head, the first node, or null for an empty tree. The tree may be any binary tree; if it happens to be a binary search tree, the list simply comes out sorted.

2How to recognise a traversal with a carried pointer

A beginner reading this question often feels it asks two hard things at once, a tree and a linked list, and tries to think about both at the same time. It helps to separate them. One part of the question says which order the nodes must be in. The other says what to do with each node once we reach it.

The first clue is the phrase in-order sequence. It names a traversal outright. When the output order is a traversal order, the traversal itself will do all the visiting for us, in exactly the right order, and we only have to decide what happens at each visit.

The second clue is the phrase doubly linked list. In a doubly linked list a node's neighbours are the node before it and the node after it. In in-order, the node before a node is simply the node we visited just before it. So at each visit, the one piece of information we need is the previously visited node.

Together these point to the trees family, and to a pattern we can call a traversal with a carried pointer. A traversal is a fixed order of visiting every node. Carrying a pointer means keeping one variable that survives from one visit to the next, here a reference called prev to the last node we visited. The general rule to carry to new problems is that when a question asks you to produce or check something in a traversal order, take the standard traversal and add a little state that passes information from each visited node to the next. Checking whether a tree is a binary search tree works the same way, carrying the previous value.

Some patterns look similar but do not fit. Linked list techniques such as reversing or merging also rewire next and previous pointers, but they assume a chain already exists. Breadth-first search, which visits the tree row by row, gives the order 10, 6, 14, 4, 8, 16, which is the wrong order. The traversal must be depth-first and in-order.

3The honest first attempt, a brute force that collects the nodes first

The honest first attempt, a brute force, separates the two jobs completely. First we run an in-order traversal and store every node, in order, in an ArrayList. Then we walk the ArrayList and set each node's left to the node before it and its right to the node after it. This is a good start, because each half is something we already know how to do.

Java
// Brute force: collect the nodes in order into a list, then link neighbours.
static Node convert(Node root) {
List<Node> nodes = new ArrayList<>();
collect(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.isEmpty() ? null : nodes.get(0);
}
static void collect(Node node, List<Node> out) { // an ordinary in-order traversal
if (node == null) return;
collect(node.left, out);
out.add(node);
collect(node.right, out);
}

Let us read it in the order it runs. collect is an ordinary recursive in-order traversal: it handles the left subtree, then adds the node, then handles the right subtree, so on our tree nodes ends up as [4, 6, 8, 10, 14, 16]. Then the for loop links neighbours. Node 4, at index 0, gets left = null and right = 6. Node 6 gets left = 4 and right = 8, and so on, until node 16 gets left = 14 and right = null. We return nodes.get(0), which is 4.

Is it safe to change the pointers in the loop? Yes, because the traversal has already finished, and we no longer need the tree's shape. That is exactly why this version feels comfortable.

Now let us count. The traversal visits six nodes and the loop links six nodes, so for n nodes the time is O(n). The space is the ArrayList of n references, O(n), plus the recursion stack, which is as deep as the tree, O(h), where h is the height.

So where is the waste? Look at what the ArrayList is used for. In the loop, nodes.get(i - 1) answers one question, which node came just before this one in in-order. During the traversal, at the moment we visit a node, the answer to that question is simply the node we visited last. We stored all n nodes to remember one at a time.

4Linking each node to the one we visited before it

So we keep only that one node. We use a reference, prev, for the last node visited in in-order, and a reference, head, for the first. When the traversal visits a node there are two cases. If prev is null, this is the very first node of the in-order sequence, the leftmost node, so it becomes head. Otherwise we join the two: prev.right = node and node.left = prev. In the language of the stage, the previous person takes this person's hand, and this person takes theirs. Then node becomes prev, and the traversal moves on.

Here comes the question every beginner should ask: we are cutting the tree's pointers while we are still walking the tree, so might we cut a pointer we still need to follow? Let us check each change.

First, node.left = prev. We do this when we visit node, and in in-order that happens only after node's entire left subtree is finished. So the old left pointer will never be followed again, and it is free to hold prev.

Second, prev.right = node. Either prev has no right child at all, or we are already inside prev's right subtree, because the call that walks it was made, with prev.right read, before we got here. In both cases nothing will read prev.right again. And node.right, which we still need to walk next, is not touched now; it changes only later, when node is prev for the following visit, by which time we have already read it. That is why the rewiring is safe.

When the traversal ends, prev is the last node, the tail. Its right pointer is still what it was in the tree, null, because the last node in in-order has no right child. And head's left is null, because the leftmost node has no left child. So the list is already closed at both ends, as the picture shows.

Step throughThe finished list, one direction at a time

The same six nodes after the conversion. A node's right pointer is its next, and its left pointer is its previous.

  1. 468101416head
    right means
    next
    head
    4

    1Following right pointers from the head, 4, gives 4, 6, 8, 10, 14, 16, and 16's right is null, which ends the list.

  2. 468101416tail
    left means
    previous
    tail
    16

    2Following left pointers from the tail, 16, gives the same nodes backwards, and 4's left is null. Both directions are linked.

A common mistake is to return the root at the end instead of head, because the root is what we were handed. But the root, 10, now sits in the middle of the list, so walking forward from it prints only 10, 14, 16, and the first three nodes seem to vanish. The head is the leftmost node, and only the first visit can tell us which one that is.

5Watching the links form on the example

The picture follows the in-order traversal on our tree, one visit per frame. Nodes the recursion is still waiting on are marked as compared, the node being visited is marked now, and nodes already linked into the list are marked done. The panel shows the link made at each visit, prev afterwards, and the list so far.

Watch prev in the panel, because it always names the node visited just before, and every link joins it to the node visited now.

Step throughIn-order conversion of the example tree, one visit at a time

Nodes the traversal is waiting on are marked as compared; the node being visited is marked now; linked nodes are marked done.

  1. 106144816
    head
    null
    prev
    null
    list
    empty

    1The traversal starts at 10 and keeps going left, through 6, down to 4, before it visits anything. head and prev are both null.

  2. 106144816
    visit
    4
    link
    none, head = 4
    prev
    4
    list
    4

    2Visit 4. prev is null, so 4 is the first node in in-order and becomes head. Then prev = 4.

  3. 106144816
    visit
    6
    link
    4 <-> 6
    prev
    6
    list
    4 <-> 6

    3Back at 6, its left subtree is finished. Visit 6: 4.right = 6 and 6.left = 4. Then prev = 6.

  4. 106144816
    visit
    8
    link
    6 <-> 8
    prev
    8
    list
    4 <-> 6 <-> 8

    4Go right to 8, which has no left child. Visit 8: 6.right = 8 and 8.left = 6. Then prev = 8.

  5. 106144816
    visit
    10
    link
    8 <-> 10
    prev
    10
    list
    4 <-> 6 <-> 8 <-> 10

    5The whole left subtree of 10 is finished. Visit 10: 8.right = 10 and 10.left = 8, linking two nodes that were not parent and child.

  6. 106144816
    visit
    14
    link
    10 <-> 14
    prev
    14
    list
    4 <-> ... <-> 10 <-> 14

    6Go right to 14, which has no left child. Visit 14: 10.right = 14 and 14.left = 10. Then prev = 14.

  7. 106144816
    visit
    16
    link
    14 <-> 16
    prev
    16
    list
    4 <-> ... <-> 14 <-> 16

    7Go right to 16. Visit 16: 14.right = 16 and 16.left = 14. Then prev = 16.

  8. 106144816
    head
    4
    tail
    16
    list
    4 <-> 6 <-> 8 <-> 10 <-> 14 <-> 16

    8The traversal ends. head is 4 and prev is 16, the tail. 4.left and 16.right were null in the tree and still are, so the list is closed at both ends.

Let us read the important frames back. In the second frame prev is null, so 4 becomes head and no link is made. From the third frame on, every visit makes exactly one link in both directions, and the list grows by one node.

The fifth frame is the one that surprises beginners. When we visit 10, prev is 8, a leaf at the bottom of the left subtree, so we link 8 <-> 10 although the two were never parent and child. The tree's shape does not matter to the list; only the in-order sequence does. In the last frame head is 4 and the tail is 16, and both ends are already null.

6The complete Java program

Here is the whole program. The node class has mutable fields, since we rewire them. The helper forward prints the list forwards and then backwards from the tail, which proves both directions are linked. main converts the example, a single node and an empty tree.

Java
public class TreeToList {
static class Node {
int val; Node left, right; // in the list: left = previous, right = next
Node(int val) { this.val = val; }
Node(int val, Node left, Node right) { this.val = val; this.left = left; this.right = right; }
}
private static Node head, prev; // first list node, and the last node linked so far
// Converts the tree in place and returns the head of the doubly linked list.
static Node convert(Node root) {
head = null;
prev = null;
inorder(root);
return head;
}
private static void inorder(Node node) {
if (node == null) return;
inorder(node.left); // everything before node is linked first
if (prev == null) head = node; // the leftmost node starts the list
else { prev.right = node; node.left = prev; } // link prev <-> node
prev = node;
inorder(node.right); // safe: node.right is not changed until later
}
static String forward(Node head) {
StringBuilder sb = new StringBuilder();
Node last = null;
for (Node n = head; n != null; last = n, n = n.right) sb.append(n.val).append(' ');
sb.append("| ");
for (Node n = last; n != null; n = n.left) sb.append(n.val).append(' '); // walk back
return sb.toString().trim();
}
public static void main(String[] args) {
// 10
// / \
// 6 14
// / \ \
// 4 8 16
Node root = new Node(10, new Node(6, new Node(4), new Node(8)), new Node(14, null, new Node(16)));
System.out.println(forward(convert(root))); // 4 6 8 10 14 16 | 16 14 10 8 6 4
System.out.println(forward(convert(new Node(7)))); // 7 | 7
System.out.println(convert(null) == null); // true
}
}

Let us read convert in the order it runs. It sets head and prev back to null, so that an earlier call cannot leave anything behind, calls inorder on the root, and returns head.

inorder first returns at once for a missing child. Otherwise it walks the left subtree completely, which links every node that comes before this one. Then it does the visit: if prev is null this node is the first and becomes head; otherwise prev.right = node and node.left = prev join the two. prev = node records that this node is now the last one linked, and finally inorder(node.right) walks the right subtree, using node.right before anything has changed it.

forward walks right pointers from the head, remembering the last node it sees, then walks left pointers back from that last node. So main prints 4 6 8 10 14 16 | 16 14 10 8 6 4, then 7 | 7 for a single node, then true, because an empty tree gives null. head and prev are static fields only to keep the example short; in real code we would keep them in a small helper object so that two conversions could not interfere.

7How much it costs, and the cases at the edges

Let us count on the example first. Six nodes were visited once each, and five links were made, each two assignments. For n nodes the time is O(n), and we cannot do better, because every node needs its pointers set.

The extra space is only the recursion stack, one waiting call per level we are below the root. Our tree is three levels deep, so at most three calls wait at once. In general it is O(h), which is O(log n) for a balanced tree and O(n) for a tree shaped like a chain. Compared with the first attempt, we no longer store the ArrayList of n references, and we never make a new node.

Comparing the ways to convert the tree
ApproachTimeExtra space
Collect nodes in an ArrayList, then linkO(n)O(n) list + O(h) stack
In-order with a prev pointerO(n)O(h) stack
Morris traversal with prevO(n)O(1)

Now the edges. An empty tree means inorder returns at once, head stays null, and convert returns null. A single node is visited once, becomes head, and keeps both pointers null, a list of one. A tree in which every node has only a left child, or only a right child, works the same way, but the recursion is then n calls deep and could overflow the stack for a very long chain; the follow-up below shows how to avoid that.

8Follow-up questions

How would you make the list circular?

After the traversal, head is the first node and prev is the last. We add two lines, head.left = prev and prev.right = head, and return head. Nothing else changes. Some versions of the question ask for this circular form, especially for binary search trees, because it lets us reach the largest value from the head in a single step backwards.

Can you do it without recursion?

Yes, with an explicit stack of our own. We push nodes while going left, then pop one, visit it by linking it with prev exactly as before, and move to its right child, repeating until the stack is empty and there is no current node. The visits come in the same in-order sequence, and the space is still O(h), but there is no risk of a StackOverflowError on a deep tree. Morris traversal goes further and uses O(1) space by threading temporary links from each node's predecessor, but it is harder to get right under pressure.

What changes if the list must be in reverse in-order, from largest to smallest?

We run the traversal the other way round, right subtree first, then the node, then the left subtree. The first node visited is then the rightmost node, which becomes head, and the same two linking lines join each node to the one before it. For our tree the list would read 16, 14, 10, 8, 6, 4.

9Conclusion

We turned a binary tree into a doubly linked list the way a group on stage turns into a line holding hands: each node's two pointers change what they hold. Collecting the nodes in an ArrayList worked, but the list only ever told us the previous node, so we kept that one node in prev and linked prev and the current node at each in-order visit, setting head on the first. It takes O(n) time and O(h) stack space and never cuts a pointer it still needs. The clue to remember is an output order that names a traversal: run that traversal and carry the previously visited node along.

What to say in the interviewAbout 90 seconds aloud

Open with

Let me check the details first: I convert in place, left becomes previous and right becomes next, the list is not circular, and I return the head, or null for an empty tree.

Then, if they want more

Since the order is in-order, the traversal does the visiting for me. A brute force would run an in-order traversal, put every node into an ArrayList, and link neighbours in a second pass. That is O(n) time but O(n) extra space, and the list only answers which node came before this one, which is just the node I visited last.

So I keep one pointer, prev, and do a recursive in-order traversal. At each visit, if prev is null, this node is the leftmost and becomes head. Otherwise I set prev dot right to the node and the node's left to prev. Then the node becomes prev and I recurse right.

The rewiring is safe because by the time I change a node's left pointer its left subtree is finished, and I never need prev's right pointer again. At the end, head's left and the tail's right are already null. This is O(n) time and O(h) space for the recursion.

An empty tree returns null and a single node becomes a list of one. For a very deep tree I would use an explicit stack, and for a circular list I would join head and tail at the end.

About this answer

Last updated
15 November 2026
Written for
Java 21, using only java.util
Checked
The Java solution was compiled and run, and each printed result matches the comment beside it. The pictures are checked against the format when the site is built. The explanation around the code is a study note: confirm anything critical against the documentation below. What the checks cover
Where the question comes from
Public interview write-ups, reworded by Scrivio; counted 17 times in that collection. How it is counted
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Convert a binary tree into a doubly linked list in in-order sequence.": https://scrivio-interview-library.netlify.app/data-structures/solutions/convert-a-binary-tree-into-a-doubly-linked-list-in-in-order-sequ-892b38/