leetcode109. 有序链表转换二叉搜索树

class Solution {
public:
    TreeNode* build(vector<int>&val,int start,int end){
        int mid=start+end>>1;
        if(start>end)return nullptr;
        TreeNode *head=new TreeNode(val[mid]);
        head->left=build(val,start,mid-1);
        head->right=build(val,mid+1,end);
        return head;
    }
    TreeNode* sortedListToBST(ListNode* head) {
        vector<int>val;
        while(head){
            val.push_back(head->val);
            head=head->next;
        }
        return build(val,0,val.size()-1);
    }
};