# [leetcode] Convert Sorted List to Binary Search Tree

Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

```/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for binary tree
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<ListNode*> listnode;
}
return _sortedListToBST(listnode, 0, listnode.size());
}
TreeNode * _sortedListToBST(vector<ListNode *>nodes, int start, int end){
if (start >= end){
return NULL;
}
else if(start + 1 == end){
TreeNode * p = new TreeNode(nodes[start]->val);
p->left = p->right = NULL;
return p;
}
else{
int mid = (end + start) / 2;
TreeNode * p = new TreeNode(nodes[mid]->val);
p->left = _sortedListToBST(nodes, start, mid);
p->right = _sortedListToBST(nodes, mid + 1, end);
return p;
}
}
};```

```/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for binary tree
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
}
TreeNode * _sortedListToBST(ListNode * head, int count){
if (count == 0){
return NULL;
}
else if(count == 1){
TreeNode * p = new TreeNode(head->val);
p->left = p->right = NULL;
return p;
}
else{
int mid = count / 2;
ListNode * midNode = getNodeByIndex(head, mid);
TreeNode * p = new TreeNode(midNode->val);
p->right = _sortedListToBST(midNode->next, count - mid - 1);
return p;
}
}
int i;
for(i = 0; head != NULL; i++){
}
return i;
}
ListNode * getNodeByIndex(ListNode * head, int idx){
while(idx--){