Convert Binary tree into Doubly Linked List

Pattern: Tree DP basics

medium

Walk the tree inorder, keeping the previous node visited. Link each node after it: prev.right = node and node.left = prev. The first node visited is the head. The tree's left and right pointers become the list's previous and next.

No verified practice link yet. The guide below states the problem in full, so you can still solve it here.

The problem

Turn the tree in place into a doubly linked list in inorder: each node's left becomes the previous node and right the next. Return the head. The tests print the list and check that every left link points back. The catalogue has no practice link for this one. Trees in the examples are written in level order, with null for a missing child, as on LeetCode.

Think first

  • In what order should the list run?
  • What do you need to remember while walking?
  • Is it safe to change a node's right pointer during the walk?

Hints

    Brute force, and why it fails

    Collect the nodes into an array in inorder, then link them: O(n) extra memory.

    The key insight

    Inorder visits nodes in list order, so linking each one to the previous visited node builds the list in one pass.

    Dry run

    inorder25 12 30 10 36 15

    Tree [10, 12, 15, 25, 30, 36]. Inorder: 25, 12, 30, 10, 36, 15.

    Step 1 of 3

    The plan

    inorder(n): if null: return
                inorder(n.left)
                if prev: prev.right = n; n.left = prev  else: head = n
                prev = n
                inorder(n.right)
    

    Complexity

    Time
    O(n)
    Space
    O(h)

    One inorder walk.

    Edge cases

    • Empty tree: an empty list.
    • One node.
    • A chain.
    I've built my logic, show the code

    Java

    class Solution {
        private TreeNode head, prev;
    
        public TreeNode bToDLL(TreeNode root) {
            head = prev = null;
            inorder(root);
            return head;
        }
    
        private void inorder(TreeNode n) {          // visit in order, linking each node after the previous one
            if (n == null) return;
            inorder(n.left);
            if (prev == null) head = n;             // the smallest-placed node is the head
            else { prev.right = n; n.left = prev; }
            prev = n;
            inorder(n.right);
        }
    }

    C++

    class Solution {
    public:
        TreeNode* head;
        TreeNode* prev;
    
        TreeNode* bToDLL(TreeNode* root) {
            head = prev = NULL;
            inorder(root);
            return head;
        }
    
        void inorder(TreeNode* n) {                 // visit in order, linking each node after the previous one
            if (n == NULL) return;
            inorder(n->left);
            if (prev == NULL) head = n;             // the first node visited is the head
            else { prev->right = n; n->left = prev; }
            prev = n;
            inorder(n->right);
        }
    };

    Python

    class Solution:
        def bToDLL(self, root):
            head = prev = None
    
            def inorder(n):                         # visit in order, linking each node after the previous one
                nonlocal head, prev
                if n is None:
                    return
                inorder(n.left)
                if prev is None:
                    head = n                        # the first node visited is the head
                else:
                    prev.right, n.left = n, prev
                prev = n
                inorder(n.right)
    
            inorder(root)
            return head

    Common mistakes

    • Linking in preorder.
    • Forgetting to set the head.

    Similar problems