Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List.
we construct from leaves to root. The idea is to insert nodes in BST in the same order as the appear in Linked List, so that the tree can be constructed in O(n) time complexity. We first count the number of nodes in the given Linked List. Let the count be n. After counting nodes, we take left n/2 nodes and recursively construct the left subtree. After left subtree is constructed, we allocate memory for root and link the left subtree with root. Finally, we recursively construct the right subtree and link it with root.
While constructing the BST, we also keep moving the list head pointer to next so that we have the appropriate pointer in each recursive call.
From http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html
Read full article from Sorted Linked List to Balanced BST | GeeksforGeeks
we construct from leaves to root. The idea is to insert nodes in BST in the same order as the appear in Linked List, so that the tree can be constructed in O(n) time complexity. We first count the number of nodes in the given Linked List. Let the count be n. After counting nodes, we take left n/2 nodes and recursively construct the left subtree. After left subtree is constructed, we allocate memory for root and link the left subtree with root. Finally, we recursively construct the right subtree and link it with root.
While constructing the BST, we also keep moving the list head pointer to next so that we have the appropriate pointer in each recursive call.
From http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html
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);
}
From http://www.darrensunny.me/leetcode-convert-sorted-list-to-binary-search-tree/
public TreeNode sortedListToBST(ListNode head) {
if (head == null)
return null;
// Get the number of all nodes
int length = 0;
for(ListNode p = head; p != null; p = p.next)
length++;
ArrayList<ListNode> current = new ArrayList<ListNode>();
current.add(head);
// Use a recursive method create the BST
return recursiveSortedListToBST(current, length);
}
private TreeNode recursiveSortedListToBST(ArrayList<ListNode> current, int num) {
if (num == 0) // Empty tree
return null;
if (num == 1) { // Single-node tree
int val = current.get(0).val;
current.set(0, current.get(0).next); // Update the list node to be used later
return new TreeNode(val);
} else {
// Recursively construct the left subtree containing (remaining-1)/2 nodes
TreeNode left = recursiveSortedListToBST(current, (num-1)/2);
// Create the root based on the current list node
TreeNode root = new TreeNode(current.get(0).val);
root.left = left;
// Update the list node to be used later, and recursively construct the right subtree
current.set(0, current.get(0).next);
root.right = recursiveSortedListToBST(current, num-1-(num-1)/2);
return root;
}
}
Also please refer to http://rleetcode.blogspot.com/2014/02/convert-sorted-list-to-binary-search.htmlRead full article from Sorted Linked List to Balanced BST | GeeksforGeeks
No comments:
Post a Comment