Advertisement
1988coder

109. Convert Sorted List to Binary Search Tree

Jan 13th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. // LeetCode URL: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
  2.  
  3. //   Definition for singly-linked list.
  4. // class ListNode {
  5. //     int val;
  6. //     ListNode next;
  7.  
  8. //     ListNode(int x) {
  9. //         val = x;
  10. //     }
  11. // }
  12.  
  13. // Definition for a binary tree node.
  14. // class TreeNode {
  15. //     int val;
  16. //     TreeNode left;
  17. //     TreeNode right;
  18.  
  19. //     TreeNode(int x) {
  20. //         val = x;
  21. //     }
  22. // }
  23.  
  24. /**
  25.  * Recursion and finding the mid point of the list
  26.  *
  27.  * Time Complexity: O(N^2). T(n) = 2 * T(n/2) + O(n/2)
  28.  *
  29.  * Space Complexity: O(H) = O(log N)
  30.  *
  31.  * N = Number of nodes on the input list. H = Height of the resultant tree.
  32.  * Since the tree is height balanced, height will be log N.
  33.  */
  34. class Solution1 {
  35.     public TreeNode sortedListToBST(ListNode head) {
  36.         if (head == null) {
  37.             return null;
  38.         }
  39.         if (head.next == null) {
  40.             return new TreeNode(head.val);
  41.         }
  42.  
  43.         return sortedListToBSTHelper(head, null);
  44.     }
  45.  
  46.     private TreeNode sortedListToBSTHelper(ListNode left, ListNode right) {
  47.         if (left == right) {
  48.             return null;
  49.         }
  50.  
  51.         ListNode fast = left;
  52.         ListNode slow = left;
  53.         while (fast != right && fast.next != right) {
  54.             fast = fast.next.next;
  55.             slow = slow.next;
  56.         }
  57.  
  58.         TreeNode subTreeHead = new TreeNode(slow.val);
  59.         subTreeHead.left = sortedListToBSTHelper(left, slow);
  60.         subTreeHead.right = sortedListToBSTHelper(slow.next, right);
  61.  
  62.         return subTreeHead;
  63.     }
  64. }
  65.  
  66. /**
  67.  * Inorder Simulation
  68.  *
  69.  * Refer:
  70.  * https://leetcode.com/articles/convert-sorted-list-to-binary-search-tree/#approach-3-inorder-simulation
  71.  *
  72.  * Time Complexity: O(N)
  73.  *
  74.  * Space Complexity: O(H) = O(log N)
  75.  *
  76.  * N = Number of nodes on the input list. H = Height of the resultant tree.
  77.  * Since the tree is height balanced, height will be log N.
  78.  */
  79. class Solution2 {
  80.     public TreeNode sortedListToBST(ListNode head) {
  81.         if (head == null) {
  82.             return null;
  83.         }
  84.         if (head.next == null) {
  85.             return new TreeNode(head.val);
  86.         }
  87.  
  88.         int listSize = getListSize(head);
  89.  
  90.         ListNode[] listNextNode = new ListNode[] { head };
  91.  
  92.         return convertListToBST(listNextNode, 0, listSize - 1);
  93.     }
  94.  
  95.     private int getListSize(ListNode head) {
  96.         ListNode cur = head;
  97.         int count = 0;
  98.         while (cur != null) {
  99.             count++;
  100.             cur = cur.next;
  101.         }
  102.         return count;
  103.     }
  104.  
  105.     private TreeNode convertListToBST(ListNode[] listNextNode, int left, int right) {
  106.         // Invalid Case
  107.         if (left > right) {
  108.             return null;
  109.         }
  110.  
  111.         // if Both left and right are equal, then leaf node is created.
  112.         if (left == right) {
  113.             return getNextTreeNode(listNextNode);
  114.         }
  115.  
  116.         int mid = left + (right - left) / 2;
  117.  
  118.         // First step of simulated inorder traversal. Recursively form the left half
  119.         TreeNode leftSubTree = convertListToBST(listNextNode, left, mid - 1);
  120.  
  121.         // Once left half is traversed, process the current node
  122.         TreeNode node = getNextTreeNode(listNextNode);
  123.  
  124.         node.left = leftSubTree;
  125.         // Recurse on the right hand side and form BST out of them
  126.         node.right = convertListToBST(listNextNode, mid + 1, right);
  127.  
  128.         return node;
  129.     }
  130.  
  131.     private TreeNode getNextTreeNode(ListNode[] listNextNode) {
  132.         TreeNode node = new TreeNode(listNextNode[0].val);
  133.         listNextNode[0] = listNextNode[0].next;
  134.         return node;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement