Medium · Trees
Convert sorted linked list to BST
Given the head of a singly linked list whose values are sorted in strictly increasing order, return the root of a height-balanced binary search tree that contains exactly those values. In a height-balanced tree, every node's two subtrees differ in height by at most 1. Any such tree is accepted, and an empty list returns null.
Examples
Example 1
[1,2,3,4,5,6,7]Output: [4,2,6,1,3,5,7]
Example 2
[-10,-3,0,5,9]Output: [0,-10,5,null,-3,null,9]
Rebuild it in the studio
Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.