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


comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9836.%20%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%8E%E5%8F%8C%E5%90%91%E9%93%BE%E8%A1%A8/README.md

面试题 36. 二叉搜索树与双向链表

题目描述

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

 

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

 

 

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

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

 

 

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

 

注意:本题与主站 426 题相同:https://leetcode.cn/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/

注意:此题对比原题有改动。

https://www.acwing.com/problem/content/description/87/

解法

方法一:中序遍历

二叉搜索树的中序遍历是有序序列,因此可以通过中序遍历得到有序序列,过程中构建双向链表。

遍历结束,将头节点和尾节点相连,返回头节点。

时间复杂度 O ( n ) O(n) O(n),空间复杂度 O ( n ) O(n) O(n)。其中 n n n 为二叉搜索树的节点个数。

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


class Solution:
    def treeToDoublyList(self, root: "Node") -> "Node":
        def dfs(root):
        	nonlocal pre
            if root is None:
            	return
            #在中序遍历中,转为双向链表
            dfs(root.left)
            if pre:pre.right = root
            root.left = pre
            pre = root
            dfs(root.right)

        head= pre = None #标记初始位置
        dfs(root)
        pre.right=root
        root.left=pre
        return head
Java
/*
// 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 {
    private Node head;
    private Node pre;

    public Node treeToDoublyList(Node root) {
        if (root == null) {
            return null;
        }
        dfs(root);
        head.left = pre;
        pre.right = head;
        return head;
    }

    private void dfs(Node root) {
        if (root == null) {
            return;
        }
        dfs(root.left);
        if (pre != null) {
            pre.right = root;
        } else {
            head = root;
        }
        root.left = pre;
        pre = root;
        dfs(root.right);
    }
}
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;
        }
        Node* pre = nullptr;
        Node* head = nullptr;
        function<void(Node*)> dfs = [&](Node* root) {
            if (!root) {
                return;
            }
            dfs(root->left);
            if (pre) {
                pre->right = root;
            } else {
                head = root;
            }
            root->left = pre;
            pre = root;
            dfs(root->right);
        };

        dfs(root);
        head->left = pre;
        pre->right = head;
        return head;
    }
};
Go
/**
 * Definition for a Node.
 * type Node struct {
 *     Val int
 *     Left *Node
 *     Right *Node
 * }
 */

func treeToDoublyList(root *Node) *Node {
	if root == nil {
		return nil
	}
	var head, pre *Node
	var dfs func(*Node)
	dfs = func(root *Node) {
		if root == nil {
			return
		}
		dfs(root.Left)
		if pre != nil {
			pre.Right = root
		} else {
			head = root
		}
		root.Left = pre
		pre = root
		dfs(root.Right)
	}
	dfs(root)
	head.Left = pre
	pre.Right = head
	return head
}
JavaScript
/**
 * // 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) {
        return null;
    }
    let head = null;
    let pre = null;
    const dfs = root => {
        if (!root) {
            return;
        }
        dfs(root.left);
        if (pre) {
            pre.right = root;
        } else {
            head = root;
        }
        root.left = pre;
        pre = root;
        dfs(root.right);
    };
    dfs(root);
    head.left = pre;
    pre.right = head;
    return head;
};
C#
/*
// Definition for a Node.
public class Node {
    public int val;
    public Node left;
    public Node right;

    public Node() {}

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

    public Node(int _val,Node _left,Node _right) {
        val = _val;
        left = _left;
        right = _right;
    }
}
*/

public class Solution {
    private Node head;
    private Node pre;

    public Node TreeToDoublyList(Node root) {
        if (root == null) {
            return null;
        }
        dfs(root);
        head.left = pre;
        pre.right = head;
        return head;
    }

    private void dfs(Node root) {
        if (root == null) {
            return;
        }
        dfs(root.left);
        if (pre != null) {
            pre.right = root;
        } else {
            head = root;
        }
        root.left = pre;
        pre = root;
        dfs(root.right);
    }
}
Swift
/* Definition for a Node.
* public class Node {
*     public var val: Int
*     public var left: Node?
*     public var right: Node?

*     public init() {
*         self.val = 0
*         self.left = nil
*         self.right = nil
*     }

*     public init(_ val: Int) {
*         self.val = val
*         self.left = nil
*         self.right = nil
*     }

*     public init(_ val: Int, _ left: Node?, _ right: Node?) {
*         self.val = val
*         self.left = left
*         self.right = right
*     }
* }
*/

class Solution {
    private var head: Node?
    private var pre: Node?

    func treeToDoublyList(_ root: Node?) -> Node? {
        if root == nil {
            return nil
        }
        dfs(root)
        head?.left = pre
        pre?.right = head
        return head
    }

    private func dfs(_ root: Node?) {
        guard let root = root else {
            return
        }
        dfs(root.left)
        if let preNode = pre {
            preNode.right = root
        } else {
            head = root
        }
        root.left = pre
        pre = root
        dfs(root.right)
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值