将已排序的双向链表转换为 BST
如何将已排序的双向链表转换为平衡二叉搜索树。
我正在考虑以与将数组转换为平衡 BST 相同的方式进行此操作。 找到中心,然后递归地转换DLL的左侧部分和右侧部分。 例如,
1 2 3 4 5
=> 1 2 (3) 4 5
=>
3
/ \
2 4
/ \
1 5
这导致递归 T(n) = 2T(n/2) + O(n)。 O(n) 用于寻找中心。 因此时间复杂度为 O(nlogn)。 我想知道是否有一种算法可以在 O(n) 内完成此操作。
How can a sorted doubly linked list be converted to a balanced binary search tree.
I was thinking of doing this the same way as converting an array to a balanced BST.
Find the centre and then recursively convert the left part and the right part of the DLL.
For example,
1 2 3 4 5
=> 1 2 (3) 4 5
=>
3
/ \
2 4
/ \
1 5
This is leads to the recurrence T(n) = 2T(n/2) + O(n). O(n) is for finding the centre.
The time complexity is therefore O(nlogn).
I was wondering if there is an algorithm that does this in O(n).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,有 O(n) 解。请注意,中序遍历 BST 是以所需的顺序迭代元素,因此只需对大小为 n 的初始空树进行中序遍历,并用列表中的元素填充它即可。 [在遍历中插入到树中的第 i 个元素是列表中的第 i 个元素]。
在答案的末尾,我添加了如何在
O(n)
中创建空平衡树。伪代码:[假设 |list| == |tree|]
复杂度很小
O(n)
,因为树的每个顶点都只有一次迭代,并且每次迭代都是 O(1)。编辑:
您可以通过简单地构建一个空的来创建一个空的平衡树完整的树(*),它是平衡的,构建它的时间复杂度为 O(n)。
(*)完全二叉树是其中每个级别(可能除了最后一个级别)都被完全填充的二叉树。
Yes there is O(n) solution. Note that an in-order traversal on a BST, is iterating the elements in the desired order, so just do an inorder traversal on an initially empty tree of size n, and fill it with elements in the list. [The i'th element you insert to the tree in your traversal, is the i'th element in the list].
At the end of the answer I added how to create an empty balanced tree in
O(n)
.pseudocode: [assuming |list| == |tree|]
Complexity is trivially
O(n)
, since there is excactly one iteration for each vertex of the tree, and each iteration is O(1).EDIT:
You can create an empty balanced tree, by simply building an empty complete tree(*), it is balanced and building it is O(n).
(*)A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled.
晚了将近4年。但这是我的功能解决方案。以下是我在 haskell 中的代码,复杂度也是
O(n)
:这实际上是我个人实践的一部分:https://github.com/HuStmpHrrr/PFDSPractise/blob/master/src/Tree/RBTree.hs#L97
almost 4 years late. but here comes my functional solution. following is my code in haskell, complexity is also
O(n)
:which is actually a part of my personal practise: https://github.com/HuStmpHrrr/PFDSPractise/blob/master/src/Tree/RBTree.hs#L97
看看我的递归插入实现(c#)。有 T(n) = 2*T(n/2) + O(1)。 O(1) 用于找到中心:(l+r)/2。所以共谋是 O(n)
我假设我们有索引数组(我们可以将链表转换为数组 O(n))
Look at my implementation of recursive insertion (c#). There are T(n) = 2*T(n/2) + O(1). O(1) is for finding the centre: (l+r)/2. So complicity is O(n)
I assume that we have indexed array (we can convert linked list to array O(n))