您的位置:首页 > 其它

将排序链表转为平衡二叉查找树

2012-09-20 10:33 246 查看
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

If you have not checked out my previous post:
Convert Sorted Array to Balanced Binary Search Tree (BST), you should check it out now as this solution is built upon the previous solution.

Things get a little more complicated when you have a singly linked list instead of an array. Please note that in linked list, you no longer have random access to an element inO(1) time.



Singly-linked lists contain nodes which have a data field as well as anext field, which points to the next node in the linked list.
Naive Solution:

A naive way is to apply the previous solution directly. In each recursive call, you would have to traverse half of the list’s length to find the middle element. The run time complexity is clearlyO(N lg
N), where N is the total number of elements in the list. This is because each level of recursive call requires a total ofN/2 traversal steps in the list, and there are a total of lg
N number of levels (ie, the height of the balanced tree).

Hint:

How about inserting nodes following the list’s order? If we can achieve this, we no longer need to find the middle element, as we are able to traverse the list while inserting nodes to the tree.

Best Solution:

As usual, the best solution requires you to think from another perspective. In other words, we no longer create nodes in the tree using the top-down approach. We create nodes bottom-up, and assign them to its parents. The bottom-up approach enables us to access
the list in its order while creating nodes.

Isn’t the bottom-up approach neat? Each time you are stucked with the top-down approach, give bottom-up a try. Although bottom-up approach is not the most natural way we think, it is extremely helpful in some cases. However, you should prefer top-down instead
of bottom-up in general, since the latter is more difficult to verify in correctness.

Below is the code for converting a singly linked list to a balanced BST. Please note that the algorithm requires the list’s length to be passed in as the function’s parameters. The list’s length could be found in O(N) time by traversing
the entire list’s once. The recursive calls traverse the list and create tree’s nodes by the list’s order, which also takesO(N) time. Therefore, the overall run time complexity is still
O(N).

BinaryTree* sortedListToBST(ListNode *& list, int start, int end) {
if (start > end) return NULL;
// same as (start+end)/2, avoids overflow
int mid = start + (end - start) / 2;
BinaryTree *leftChild = sortedListToBST(list, start, mid-1);
BinaryTree *parent = new BinaryTree(list->data);
parent->left = leftChild;
list = list->next;
parent->right = sortedListToBST(list, mid+1, end);
return parent;
}

BinaryTree* sortedListToBST(ListNode *head, int n) {
return sortedListToBST(head, 0, n-1);
}

http://www.leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: