im a leetcode beginner.This problem is quite similar with "109. Convert Sorted List to Binary Search Tree". So i use a vector to store the elements in the list and solve it recursively like problem 109, but my code went wrong. Can anyone help me out? Thanks a lot.

Here's my code:

class Solution {

public:

TreeNode* sortedListToBST(ListNode* head) {

TreeNode* root;

vector<int> temp;

int i=0;

if(!head)

return nullptr;

while(head){

temp[i++]=head->val;

head=head->next;

}

root=solve(temp,0,temp.size()-1);

return root;

}

TreeNode* solve(vector<int>&vec,int begin,int end){

int mid=(begin+end)/2;

if(begin>end)

return nullptr;

TreeNode* ptr=new TreeNode(vec[mid]);

ptr->left=solve(vec,begin,mid-1);

ptr->right=solve(vec,mid+1,end);

return ptr;

}

};