# Convert Sorted List to Binary Search Tree LeetCode Solution

Here, We see Convert Sorted List to Binary Search Tree problem Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

## Problem Statement ->

Given the head of a singly linked list where elements are sorted in ascending order, convert it to a  height-balanced binary search tree.

```Example 1: (fig-1)
Output: [0,-3,9,-10,null,5]
Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.

Example 2:
Output: []```

## Convert Sorted List to Binary Search Tree Leetcode Solution C++ ->

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```class Solution {
public:
while(fast && fast->next) {
pre = slow;
slow = slow->next;
fast = fast->next->next;
}
pre->next = 0; // break two halves

TreeNode* root = new TreeNode(slow->val);
root->right = sortedListToBST(slow->next);

return root;
}
};
```Code language: C++ (cpp)```

## Convert Sorted List to Binary Search Tree Leetcode Solution Java ->

``````class Solution {
}
public TreeNode toBST(ListNode head, ListNode tail){

while(fast!=tail&&fast.next!=tail){
fast = fast.next.next;
slow = slow.next;
}
}
}
```Code language: Java (java)```

## Convert Sorted List to Binary Search Tree Leetcode Solution JavaScript ->

``````var sortedListToBST = function(head) {
let curr = head, count = 0
while (curr) curr = curr.next, count++
const treeify = (i, j) => {
if (j < i) return null
let mid = i + j >> 1, node = new TreeNode()
node.left = treeify(i, mid - 1)
node.val = curr.val, curr = curr.next
node.right = treeify(mid + 1, j)
return node
}
return treeify(1, count)
};
```Code language: JavaScript (javascript)```

## Convert Sorted List to Binary Search Tree Leetcode Solution Python ->

``````class Solution(object):
return
while fast and fast.next:
fast = fast.next.next
slow = slow.next

tmp = slow.next

slow.next = None
root = TreeNode(tmp.val)
root.right = self.sortedListToBST(tmp.next)
return root
```Code language: Python (python)```

#### Longest Valid Parentheses LeetCode Solution

Given a string containing just the characters ‘(‘ and ‘)’, return the length of the…

#### Nim Game LeetCode Solution

Given n, the number of stones in the heap, return true if you can win the game assuming…

#### Climbing Stairs LeetCode Solution

You are climbing a staircase. It takes n steps to reach the top. Each time you can…

#### Merge Two Sorted Lists LeetCode Solution

You are given the heads of two sorted linked lists list1 and list2. Merge the…

#### Permutations II LeetCode Solution

Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in…

#### Jump Game II LeetCode Solution

You are given a 0-indexed array of integers nums of length n. You are initially…