TreeNode *buildTree(ListNode* head, ListNode *end){
if(head == NULL || head == end)
return NULL;
ListNode* fast = head;
ListNode* slow = head;
//get the middle
//notice: != end, not != NULL
while(fast != end && fast->next != end){
fast = fast->next->next;
slow = slow->next;
}
TreeNode *root = new TreeNode(slow->val);
root->left = buildTree(head, slow);
root->right = buildTree(slow->next, end);
return root;
}
TreeNode *sortedListToBST(ListNode *head) {
if(head == NULL)
return NULL;
return buildTree(head, NULL);
}【leetcode】Convert Sorted List to Binary Search Tree,布布扣,bubuko.com
【leetcode】Convert Sorted List to Binary Search Tree
原文:http://blog.csdn.net/shiquxinkong/article/details/29590891