将排序链表转为平衡二叉查找树
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).
http://www.leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html
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
相关文章推荐
- 将排序数组转为平衡二叉查找树
- 链表——有序链表化为平衡二叉查找树
- [程序员面试题精选100题(01)]-二叉查找树转成排序的双向链表
- [程序员面试题精选100题]1.把二叉查找树转变成排序的双向链表
- 二叉查找树 转换成 排序的双向链表
- 把二叉查找树转变成排序的双向链表
- 微软面试100题系列---把二叉查找树转换成排序的双向链表
- 数据结构——树——二叉查找树转换成排序的循环双向链表
- 算法 把二叉查找树转变成排序的双向链表
- 把二叉查找树转变成排序的双向链表
- 二叉查找树转变为排序的双向链表
- 二叉查找树转为双向链表
- Convert Sorted List to Binary Search Tree (递增的链表转化成高度平衡的二叉查找树)【leetcode】
- 题目编号001:二叉查找树转变成排序的双向链表
- 二叉查找树转换成排序的双向链表
- [LeetCode] 从排序的单链表到平衡搜索二叉树
- 二叉查找树转变成排序的双向链表
- AVL树(平衡二叉查找树)
- Leetcode#108. Convert Sorted Array to Binary Search Tree (平衡二叉查找树)
- 单链表插入排序