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连起来。


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

        return leftHead;
    }
};
  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值