LintCode 1534 · Convert Binary Search Tree to Sorted Doubly Linked List (二叉树转双链表好题)

1534 · Convert Binary Search Tree to Sorted Doubly Linked List
Algorithms
Medium

Description
Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list.

Let’s take the following BST as an example, it may help you understand the problem better:

bstdlloriginalbst

We want to transform this BST into a circular doubly linked list. Each node in a doubly linked list has a predecessor and successor. 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.

The figure below shows the circular doubly linked list for the BST above. The “head” symbol means the node it points to is the smallest element of the linked list.

Specifically, 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. We should return the pointer to the first element of the linked list.

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

Example
Example 1:

Input: {4,2,5,1,3}
4
/
2 5
/
1 3
Output: “left:1->5->4->3->2 right:1->2->3->4->5”
Explanation:
Left: reverse output
Right: positive sequence output
Example 2:

Input: {2,1,3}
2
/
1 3
Output: “left:1->3->2 right:1->2->3”
Related Knowledge
学习《常用算法 25 讲》课程中的3.5二叉排序树:如何动态查找第k大元素?相关内容 ,了解更多相关知识!

解法1:中序遍历

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: root of a tree
     * @return: head node of a doubly linked list
     */
    TreeNode * treeToDoublyList(TreeNode * root) {
           inOrderTraversal(root);
        if (head) head->left = next;
        if (next) next->right = head;
        return head;
    }
private:
    TreeNode *pre = NULL, *head = NULL, *next = NULL;
    void inOrderTraversal(TreeNode *root) {
        if (!root) return;
        inOrderTraversal(root->left);
        if (!head) {
            head = root;
        }
        if (!pre) {
            pre = root;
            next = root; //要加这个for single node tree, 不然{1}出错。
        } else {
            pre->right = root;
            root->left = pre;
            pre = root;
            next = root;
        }
        inOrderTraversal(root->right);
    }
};

二刷:其实上面的next不需要,因为pre最后也就是next。

class Solution {
public:
    /**
     * @param root: root of a tree
     * @return: head node of a doubly linked list
     */
    TreeNode * treeToDoublyList(TreeNode * root) {
        inOrderTravel(root);
        pre->right = head;
        head->left = pre;
        return head;
    }
private:
    void inOrderTravel(TreeNode * root) {
        if (!root) return;
        inOrderTravel(root->left);
        if (!pre) {
            pre = root;
            head = root;
        } else {
            pre->right = root;
            root->left = pre;
            pre = root;
        }
        inOrderTravel(root->right);
    }
    TreeNode *pre = NULL, *head = NULL;
    
};

解法2:用后序遍历。左右子树分别处理,然后中间用root连起来。TBD