Convert Binary Search Tree to Sorted Doubly Linked List - shilpathota/99-leetcode-solutions GitHub Wiki

Convert Binary Search Tree to Sorted Doubly Linked List

Leet Code Link - https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/description/

Complexity - Medium

Description

Convert a Binary Search Tree to a sorted Circular Doubly-Linked List in place.

You can think of the left and right pointers as synonymous to the predecessor and successor pointers in a doubly-linked list. For a circular doubly linked list, the predecessor of the first element is the last element, and the successor of the last element is the first element.

We want to do the transformation in place. After the transformation, the left pointer of the tree node should point to its predecessor, and the right pointer should point to its successor. You should return the pointer to the smallest element of the linked list.

Example 1:

Input: root = [4,2,5,1,3]
Output: [1,2,3,4,5]

Explanation: The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor relationship.

Example 2:

Input: root = [2,1,3]
Output: [1,2,3]

Constraints:

The number of nodes in the tree is in the range [0, 2000].
-1000 <= Node.val <= 1000
All the values of the tree are unique.

Solution

  • Initiate the first and the last nodes as nulls.

  • Call the standard inorder recursion helper(root) :

  • If the node is not null :

  • Call the recursion for the left subtree helper(node.left).

  • If the last node is not null, link the last and the current node nodes.

  • Else initiate the first node.

  • Mark the current node as the last one: last = node.

  • Call the recursion for the right subtree helper(node.right).

  • Link the first and the last nodes to close the DLL ring and then return the first node

/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val,Node _left,Node _right) {
        val = _val;
        left = _left;
        right = _right;
    }
};
*/

class Solution {
    Node first = null;
    Node last=null;
    public void inorder(Node root){
        if(root != null){
        inorder(root.left);
        if(last!=null){
            last.right = root;
            root.left = last;
        }else{
            first = root;
        }
        last = root;

        inorder(root.right);
        }
    }
    public Node treeToDoublyList(Node root) {
                if(root == null) return null;

        inorder(root);
        last.right = first;
        first.left = last;
        return first;
    }
}

Complexity

Time complexity : O(N) since each node is processed exactly once.

Space complexity : O(N). We have to keep a recursion stack of the size of the tree height, which is O(logN) for the best case of a completely balanced tree and O(N) for the worst case of a completely unbalanced tree.