The Convert Sorted List to Binary Search Tree coding problem is a more challenging version of the array-to-BST problem. Here, you are given a singly linked list where values are sorted in ascending order. You must convert it into a height-balanced BST. The difficulty arises from the fact that you cannot access the middle element of a linked list in O(1) time like you can with an array.
Uber, Meta, and Google use the Convert Sorted List to Binary Search Tree interview question to see if a candidate can optimize a recursive algorithm for a less flexible data structure. It tests your ability to handle two pointers (Slow and Fast) to find the middle, or your ability to use a more advanced "in-order simulation" to build the tree while traversing the list once.
There are two common Tree interview patterns for this:
List: 1 -> 2 -> 3 -> 4 -> 5
Using Fast/Slow Pointers:
3.3 becomes the root.[1, 2] and [4, 5].[1, 2]. Slow finds 1 (or 2).The O(N) in-order simulation is highly impressive. It involves keeping a global pointer to the current list node and moving it forward every time a tree node is "created" during the recursive calls.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Convert Sorted Array to Binary Search Tree | Easy | Solve | |
| Insert into a Binary Search Tree | Medium | Solve | |
| Delete Node in a BST | Medium | Solve | |
| Inorder Successor in BST II | Medium | Solve | |
| Balance a Binary Search Tree | Medium | Solve |