class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
return sortedListToBST(head,NULL);
}
TreeNode* sortedListToBST(ListNode* head,ListNode* tail){
if(head==tail)
return NULL;
ListNode* mid=head,*fast=head;
while(fast!=tail && fast->next!=tail){
mid=mid->next;
fast=fast->next->next;
}
TreeNode* root=new TreeNode(mid->val);
root->left=sortedListToBST(head,mid);
root->right=sortedListToBST(mid->next,tail);
return root;
}
};