http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
Example:
Input: Linked List 1->2->3
Output: A Balanced BST
2
/
1 3
Input: Linked List 1->2->3->4->5->6->7
Output: A Balanced BST
4
/
2 6
/ /
1 3 4 7
Input: Linked List 1->2->3->4
Output: A Balanced BST
3
/
2 4
/
1
Input: Linked List 1->2->3->4->5->6
Output: A Balanced BST
4
/
2 6
/ /
1 3 5
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; next = null; } 7 * } 8 */ 9 /** 10 * Definition for binary tree 11 * public class TreeNode { 12 * int val; 13 * TreeNode left; 14 * TreeNode right; 15 * TreeNode(int x) { val = x; } 16 * } 17 */ 18 public class Solution { 19 public TreeNode sortedListToBST(ListNode head) { 20 21 if (head == null) 22 return null; 23 int count = 0; 24 ListNode l1 = head; 25 while (l1 != null) { 26 count++; 27 l1 = l1.next; 28 } 29 return sortedListToBST_helper(head, count); 30 } 31 32 public ListNode getMid(ListNode head, int n) { 33 ListNode l1 = head; 34 while (n-- > 0) { 35 l1 = l1.next; 36 } 37 return l1; 38 } 39 40 public TreeNode sortedListToBST_helper(ListNode head, int count) { 41 if (count < 1) 42 return null; 43 int mid = count / 2; 44 ListNode mid_node = getMid(head, count / 2); 45 TreeNode root = new TreeNode(mid_node.val); 46 ListNode mid_node_pre = getMid(head, count / 2 - 1); 47 mid_node_pre.next = null; 48 root.left = sortedListToBST_helper(head, count / 2); 49 root.right = sortedListToBST_helper(mid_node.next, count - mid - 1); 50 return root; 51 } 52 }
此解法时间复杂度为O(nlog(n))
关于Java传值还是传引用
1. 对象就是传引用
2. 原始类型就是传值
此题另一种decent的解法:
1 /* This function counts the number of nodes in Linked List and then calls 2 sortedListToBSTRecur() to construct BST */ 3 struct TNode* sortedListToBST(struct LNode *head) 4 { 5 /*Count the number of nodes in Linked List */ 6 int n = countLNodes(head); 7 8 /* Construct BST */ 9 return sortedListToBSTRecur(&head, n); 10 } 11 12 /* The main function that constructs balanced BST and returns root of it. 13 head_ref --> Pointer to pointer to head node of linked list 14 n --> No. of nodes in Linked List */ 15 struct TNode* sortedListToBSTRecur(struct LNode **head_ref, int n) 16 { 17 /* Base Case */ 18 if (n <= 0) 19 return NULL; 20 21 /* Recursively construct the left subtree */ 22 struct TNode *left = sortedListToBSTRecur(head_ref, n/2); 23 24 /* Allocate memory for root, and link the above constructed left 25 subtree with root */ 26 struct TNode *root = newNode((*head_ref)->data); 27 root->left = left; 28 29 /* Change head pointer of Linked List for parent recursive calls */ 30 *head_ref = (*head_ref)->next; 31 32 /* Recursively construct the right subtree and link it with root 33 The number of nodes in right subtree is total nodes - nodes in 34 left subtree - 1 (for root) which is n-n/2-1*/ 35 root->right = sortedListToBSTRecur(head_ref, n-n/2-1); 36 37 return root; 38 } 39 /* UTILITY FUNCTIONS */ 40 41 /* A utility function that returns count of nodes in a given Linked List */ 42 int countLNodes(struct LNode *head) 43 { 44 int count = 0; 45 struct LNode *temp = head; 46 while(temp) 47 { 48 temp = temp->next; 49 count++; 50 } 51 return count; 52 }
此解法时间复杂度位O(n)
我尝试用Java来写。由于Java和C系列的指针不大一样。失败了。。以后再改进吧。