Binary Tree to DLL

Hard Trees & BST Tree to Lists and vice-versa Original on GeeksforGeeks

Reported atMetaApple

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, Python, C++ and JavaScript, 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 1 to 10^5 nodes.
  • Do it in place: left becomes prev and right becomes next.

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 4 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

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 code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Collect nodes, then linkO(n)O(n)
Optimal (inorder with a prev pointer)O(n)O(h)

2Optimal (inorder with a prev pointer)

TimeO(n)
SpaceO(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.

  1. go(left).
  2. If prev is null, head = cur; else prev.right = cur, cur.left = prev. prev = cur.
  3. 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.