# Leetcode Convert Sorted List to Binary Search Tree problem solution

In this Leetcode Convert Sorted List to Binary Search Tree problem solution we have Given the head of a singly linked list where elements are sorted in ascending order, convert to a height-balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differs by more than 1.

## Problem solution in Python.

return None

while fast and fast.next:
prev = slow
slow = slow.next
fast = fast.next.next

tmp = prev.next
prev.next = None
node = TreeNode(slow.val)
node.right = self.DFS(slow.next)
return node

## Problem solution in Java.

class Solution {
return null;
else
}
public static TreeNode BST(ListNode head,ListNode tail){
return null;
while(fast!=tail&&fast.next!=tail){
slow=slow.next;
fast=fast.next.next;
}
TreeNode root=new TreeNode(slow.val);
root.right=BST(slow.next,tail);
return root;
}
}

## Problem solution in C++.

class Solution {
public:

return NULL;
while(fast and fast->next)
{
tt=slow;
slow=slow->next;
fast=fast->next->next;
}
if(tt)
tt->next=NULL;
else

TreeNode *curr=new TreeNode(slow->val);
TreeNode *r=sortedListToBST(slow->next);
curr->left=l;curr->right=r;
return curr;
}
};

## Problem solution in C.

struct TreeNode* get(int v){
struct TreeNode* x;
x=(struct TreeNode*)malloc(sizeof(struct TreeNode));
x->val=v;
x->left=NULL;
x->right=NULL;
return x;
}
struct TreeNode* func(int *b,int l,int h){
struct TreeNode* root;
int m=(l+h)/2;
if(h>=l){
root=get(b[m]);
root->left=func(b,l,m-1);
root->right=func(b,m+1,h);
return root;
}
else
return NULL;
}

struct TreeNode* t;
int a=pow(10,4);
int b[2*a];
int i=0,j;