class Solution {
public:
TreeNode *sortedListToBST(ListNode *head) {
ListNode *p;
int count=0;
p=head;
while(p!=NULL)
{
count++;
p=p->next;
}
return sorta(head,0,count-1);
}
TreeNode *sorta(ListNode *&head,int start,int end)
{
if(start>end)
return NULL;
int mid=(start+end)/2;
TreeNode *left=sorta(head,start,mid-1);
TreeNode *root=new TreeNode(head->val);
head=head->next;
TreeNode *right=sorta(head,mid+1,end);
root->left=left;
root->right=right;
return root;
}
};