剑指 Offer 36. 二叉搜索树与双向链表

输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的循环双向链表。要求不能创建任何新的节点,只能调整树中节点指针的指向

为了让您更好地理解问题,以下面的二叉搜索树为例

我们希望将这个二叉搜索树转化为双向循环链表。链表中的每个节点都有一个前驱和后继指针。对于双向循环链表,第一个节点的前驱是最后一个节点,最后一个节点的后继是第一个节点。

下图展示了上面的二叉搜索树转化成的链表。“head” 表示指向链表中有最小元素的节点。

 特别地,我们希望可以就地完成转换操作。当转化完成以后,树中节点的左指针需要指向前驱,树中节点的右指针需要指向后继。还需要返回链表中的第一个节点的指针。

解法:中序遍历后重组链表就可以了。

/*
// 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 {
    ArrayList<Node> res = new ArrayList<Node>();
    public Node treeToDoublyList(Node root) {
        inorder(root);
        Node head = res.get(0);

        Node cur = res.get(0);
        Node next;
        for(int i = 1;i< res.size() -1;i++)
        {
            next = res.get(i);
            cur.right = next;
            next.left = cur;
            cur =next;
        }        
        next = res.get(res.size()-1);
        cur.right = next;
        next.right = head;
        next.left = cur;
        head.left=next;
        return head;        
    }

    public void inorder(Node root)
    {
        if(root == null) return;
        inorder(root.left);
        res.add(root);
        inorder(root.right);
    }
}
class Solution {
    Node pre, head;
    public Node treeToDoublyList(Node root) {
        if(root == null) return null;
        dfs(root);
        head.left = pre;
        pre.right = head;
        return head;
    }
    void dfs(Node cur) {
        if(cur == null) return;
        dfs(cur.left);
        if(pre != null) pre.right = cur;
        else head = cur;
        cur.left = pre;
        pre = cur;
        dfs(cur.right);
    }
}
原文地址:https://www.cnblogs.com/kpwong/p/14689237.html