本问题对应的 leetcode 原文链接:剑指 Offer 36. 二叉搜索树与双向链表

问题描述

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

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

36

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

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

36-2

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

解题思路

视频讲解直达: 本题视频讲解

代码实现

class Solution {
    public Node treeToDoublyList(Node root) {
        if(root == null){
            return null;
        }

        Queue<Node> queue = new LinkedList<>();
        inOrder(root, queue);
        Node head = queue.poll();
        Node pre = head;

        while(!queue.isEmpty()){
            Node cur = queue.poll();
            pre.right = cur;
            cur.left = pre;
            pre = cur;
        }

        pre.right = head;
        head.left = pre;

        return head;
    }

    void inOrder(Node root, Queue<Node> queue){
        if(root == null){
            return;
        }
        inOrder(root.left, queue);
        queue.add(root);
        inOrder(root.right, queue);
    }
}

Python

# Definition for a Node.
class Node(object):
    def __init__(self, val, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution(object):
    def treeToDoublyList(self, root):
        """
        :type root: Node
        :rtype: Node
        """
        if not root:
            return None

        queue = []
        self.inOrder(root, queue)
        head = queue.pop(0)
        pre = head

        while queue:
            cur = queue.pop(0)
            pre.right = cur
            cur.left = pre
            pre = cur

        pre.right = head
        head.left = pre

        return head

    def inOrder(self, root, queue):
        if not root:
            return
        self.inOrder(root.left, queue)
        queue.append(root)
        self.inOrder(root.right, queue)

C++

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

    Node() {}

    Node(int _val) {
        val = _val;
        left = NULL;
        right = NULL;
    }

    Node(int _val, Node* _left, Node* _right) {
        val = _val;
        left = _left;
        right = _right;
    }
};
*/
class Solution {
public:
    Node* treeToDoublyList(Node* root) {
        if (!root) {
            return nullptr;
        }

        vector<Node*> queue;
        inOrder(root, queue);
        Node* head = queue[0];
        Node* pre = head;

        for (int i = 1; i < queue.size(); i++) {
            Node* cur = queue[i];
            pre->right = cur;
            cur->left = pre;
            pre = cur;
        }

        pre->right = head;
        head->left = pre;

        return head;
    }

    void inOrder(Node* root, vector<Node*>& queue) {
        if (!root) {
            return;
        }
        inOrder(root->left, queue);
        queue.push_back(root);
        inOrder(root->right, queue);
    }
};

Go

/**
 * Definition for a Node.
 * type Node struct {
 *     Val int
 *     Next *Node
 *     Random *Node
 * }
 */
func copyRandomList(head *Node) *Node {
    if head == nil {
        return nil
    }
    // 复制链表节点
    cur := head
    for cur != nil {
        next := cur.Next
        cur.Next = &Node{Val: cur.Val}
        cur.Next.Next = next
        cur = next
    }

    // 复制随机节点
    cur = head
    for cur != nil {
        curNew := cur.Next
        if cur.Random != nil {
            curNew.Random = cur.Random.Next
        }
        cur = cur.Next.Next
    }

    // 拆分,比如把 A->A1->B->B1->C->C1拆分成 A->B->C和A1->B1->C1
    headNew := head.Next
    cur = head
    curNew := head.Next
    for cur != nil {
        cur.Next = cur.Next.Next
        cur = cur.Next
        if cur == nil {
            curNew.Next = nil
        } else {
            curNew.Next = cur.Next
        }
        curNew = curNew.Next
    }

    return headNew
}

JS

/**
 * // Definition for a Node.
 * function Node(val,left,right) {
 *    this.val = val;
 *    this.left = left;
 *    this.right = right;
 * };
 */
/**
 * @param {Node} root
 * @return {Node}
 */
var treeToDoublyList = function (root) {
  if (root == null) {
    return null;
  }

  var queue = [];
  inOrder(root, queue);
  var head = queue.shift();
  var pre = head;

  while (queue.length !== 0) {
    var cur = queue.shift();
    pre.right = cur;
    cur.left = pre;
    pre = cur;
  }

  pre.right = head;
  head.left = pre;

  return head;

  function inOrder(root, queue) {
    if (root == null) {
      return;
    }
    inOrder(root.left, queue);
    queue.push(root);
    inOrder(root.right, queue);
  }
};

发表回复

后才能评论