Sorted Linked List to Balanced BST | GeeksforGeeks



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
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.html
Read full article from Sorted Linked List to Balanced BST | GeeksforGeeks

No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts