Convert Sorted List to BST
Master this topic with zero to advance depth.
Convert Sorted List to BST
Convert a sorted linked list to a height-balanced BST.
Visual Representation
List: -10 -> -3 -> 0 -> 5 -> 9
Root is 0.Approach 1
Level I: Brute Force (Convert to Array)
Intuition
Convert the linked list into an array. Then, use a standard recursive algorithm to convert the sorted array into a height-balanced BST by picking the middle element as the root.
⏱ O(N)💾 O(N)
Detailed Dry Run
List: -10, -3, 0, 5, 9. Array: [-10, -3, 0, 5, 9].
- Root = 0.
- Left = [-10, -3].
- Right = [5, 9].
Approach 2
Level III: Recursive (Middle find)
Intuition
Use slow/fast pointers to find the middle (root), split, and recurse.
⏱ O(N log N)💾 O(log N)
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.