Formatted question description: https://leetcode.ca/all/426.html

426. Convert Binary Search Tree to Sorted Doubly Linked List

Level

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:

Image text

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

Image text

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.

Image text

Example 2:

Input: root = [2,1,3]

Output: [1,2,3]

Example 3:

Input: root = []

Output: []

Explanation: Input is an empty tree. Output is also an empty Linked List.

Example 4:

Input: root = [1]

Output: [1]

Constraints:

  • -1000 <= Node.val <= 1000
  • Node.left.val < Node.val < Node.right.val
  • All values of Node.val are unique.
  • 0 <= Number of Nodes <= 2000

Solution

If the root is null, simply return the root. If there is only one node in the binary search tree, then let the root’s predecessor and successor both point to the root itself, and return the root.

Use the idea of the solution to problem 94. Consider inorder traversal of the binary search tree, which contains all the elements sorted in ascending order.

However, this problem does not require returning a list of inorder traversal result. Therefore, when doing inorder traversal, maintain a previous node, which is initially null. Each time a node is met, make the previous node’s successor be the current node, and make the current node’s predecessor be the previous node.

After all the nodes are visited, all the relations between each pair of adjacent nodes are maintained. Since the list is required to be circular, make the head node’s predecessor be the tail node, and make the tail node’s successor be the head node. Finally, return the head 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 {
    public Node treeToDoublyList(Node root) {
        if (root == null)
            return root;
        if (root.left == null && root.right == null) {
            root.left = root;
            root.right = root;
            return root;
        }
        Node head = root, tail = root;
        while (head.left != null)
            head = head.left;
        while (tail.right != null)
            tail = tail.right;
        inorderTraversal(root);
        head.left = tail;
        tail.right = head;
        return head;
    }

    public void inorderTraversal(Node root) {
        Stack<Node> stack = new Stack<Node>();
        Node node = root;
        Node prevNode = null;
        while (!stack.isEmpty() || node != null) {
            while (node != null) {
                stack.push(node);
                node = node.left;
            }
            Node visitNode = stack.pop();
            visitNode.left = prevNode;
            if (prevNode != null)
                prevNode.right = visitNode;
            node = visitNode.right;
            prevNode = visitNode;
        }
    }
}

All Problems

All Solutions