Convert Sorted List to Binary Search Tree - Problem
Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height-balanced binary search tree.
A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than 1.
The resulting BST should maintain the sorted order property where for each node:
- All values in the left subtree are less than the node's value
- All values in the right subtree are greater than the node's value
Input & Output
Example 1 — Basic Conversion
$
Input:
head = [-10,-3,0,5,9]
›
Output:
[0,-3,9,-10,null,5]
💡 Note:
One possible balanced BST: root=0, left subtree has -3 with -10 as left child, right subtree has 9 with 5 as left child
Example 2 — Empty List
$
Input:
head = []
›
Output:
[]
💡 Note:
Empty linked list results in null tree
Example 3 — Single Node
$
Input:
head = [1]
›
Output:
[1]
💡 Note:
Single node becomes the root with no children
Constraints
- 0 ≤ Number of nodes ≤ 2 × 104
- -105 ≤ Node.val ≤ 105
- The linked list is sorted in ascending order
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code