Skip to main content

Convert Sorted List to Binary Search Tree - Solution & Explanation

MediumLinked ListDivide and ConquerTreeBinary Search Tree18 min readAsked at: Amazon, Microsoft, Apple +6
Practice this problem

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:

Input: head = [-10,-3,0,5,9]
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:

Input: head = []
Output: []

 

Constraints:

  • The number of nodes in head is in the range [0, 2 * 104].
  • -105 <= Node.val <= 105

Approach Overview

Problem Overview: You receive the head of a sorted singly linked list and must convert it into a height-balanced binary search tree. The BST property must hold (left < root < right) while keeping the tree balanced so operations remain efficient.

Approach 1: Convert Linked List to Array (O(n) time, O(n) space)

The simplest strategy is to copy the linked list values into an array. Once stored in an array, the problem becomes identical to building a balanced BST from a sorted array. Recursively choose the middle index as the root, build the left subtree from the left half, and the right subtree from the right half. Array indexing gives constant-time access to the middle element, so the recursion runs in O(n) time with O(n) extra space for the array. This approach is easy to reason about and often preferred when clarity matters more than strict memory usage.

Approach 2: Fast and Slow Pointer (O(n log n) time, O(log n) space)

This approach avoids converting the list into an array. Use the classic fast and slow pointer technique to locate the middle node of the linked list, which becomes the root of the BST. Recursively apply the same logic to the left portion of the list (before the middle) and the right portion (after the middle). Finding the middle node takes linear time each recursion level, and the recursion depth is O(log n), producing a total time complexity of O(n log n). Only recursion stack space is used, so extra space is O(log n). This method demonstrates deeper understanding of linked list traversal and divide and conquer.

Recommended for interviews: Interviewers typically expect the fast–slow pointer solution because it shows you understand how to manipulate a linked list without converting it to another structure. Starting with the array approach is still valuable—it proves you recognize the relationship between a sorted sequence and a balanced binary search tree. Then improving it to the in-place divide-and-conquer version demonstrates stronger problem-solving skills.

Approach 1: Approach 1: Convert Linked List to Array

This approach involves converting the linked list to an array and then using the middle of the array to create a height-balanced binary search tree (BST). The middle of the array will become the root, and this concept is applied recursively to the left and right halves of the array.

The solution first converts the linked list into an array for easy access. It then uses the middle element of the array as the root of the BST and recursively applies this method to the left and right segments of the list. This way, a height-balanced BST is created.

Code

Python

JavaScript

Complexity

Time Complexity: O(n), where n is the number of nodes in the linked list, because we traverse the list to create the array.
Space Complexity: O(n), for storing the values in the array.

Try this approach in the editor →

Approach 2: Approach 2: Fast and Slow Pointer

This approach uses the slow and fast pointer technique to find the middle of the linked list, which forms the root of the BST. The linked list is recursively divided into two halves, and this method is used to create the left and right subtrees.

The Java solution uses a recursive function, with fast and slow pointers to find the middle of the linked list. This middle element becomes the root of the BST, and the process is applied to the list segments left and right of the middle to form the subtrees.

Code

Java

C++

Complexity

Time Complexity: O(n log n), because cutting the list in half at each step takes O(log n) splits, and each split involves a linear scan.
Space Complexity: O(log n), for the recursion stack where n is the number of nodes in the list.

Try this approach in the editor →

Approach 3: DFS

We first convert the linked list to an array nums, and then use depth-first search to construct the binary search tree.

We define a function dfs(i, j), where i and j represent the current interval [i, j]. Each time, we choose the number at the middle position mid of the interval as the root node, recursively construct the left subtree for the interval [i, mid - 1], and the right subtree for the interval [mid + 1, j]. Finally, we return the node corresponding to mid as the root node of the current subtree.

In the main function, we just need to call dfs(0, n - 1) and return the result.

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the linked list.

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Convert Linked List to Array

Time Complexity: O(n), where n is the number of nodes in the linked list, because we traverse the list to create the array.
Space Complexity: O(n), for storing the values in the array.

Approach 2: Fast and Slow Pointer

Time Complexity: O(n log n), because cutting the list in half at each step takes O(log n) splits, and each split involves a linear scan.
Space Complexity: O(log n), for the recursion stack where n is the number of nodes in the list.

DFS

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Convert Linked List to ArrayO(n)O(n)When simplicity matters and extra memory is acceptable
Fast and Slow Pointer (Divide and Conquer)O(n log n)O(log n)When you want an in-place linked list solution with minimal extra memory

Video Solution

Convert Sorted List to Binary Search Tree | Google | Flipkart | Amazon | Leetcode 109codestorywithMIK18,406 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Convert Sorted List to Binary Search Tree easy or hard?
Convert Sorted List to Binary Search Tree is rated Medium on LeetCode. The main challenge is recognizing that the middle element must become the root to maintain balance while handling linked list traversal efficiently.
How to solve Convert Sorted List to Binary Search Tree in O(n)?
Achieving O(n) time typically involves first copying the linked list values into an array. Once stored in an array, you can build a balanced BST by recursively choosing the middle element as the root. Each element is processed once, producing overall O(n) time.
Convert Sorted List to Binary Search Tree Python or Java solution?
Python and Java solutions usually follow two patterns: convert the linked list to an array and build the BST using the middle index, or use fast and slow pointers to find the middle node directly in the list. Both approaches recursively construct left and right subtrees.
What is the best approach for Convert Sorted List to Binary Search Tree?
The fast and slow pointer approach is usually considered the best interview solution. It finds the middle node of the linked list to use as the root and recursively builds left and right subtrees. This method avoids converting the list into another structure and uses only O(log n) recursion space.
What data structure is used in Convert Sorted List to Binary Search Tree?
The problem primarily uses a singly linked list as input and constructs a binary search tree as output. Solutions commonly rely on recursion, the fast–slow pointer technique, and divide-and-conquer to maintain BST ordering and balance.
What is the time complexity of Convert Sorted List to Binary Search Tree?
Time complexity depends on the approach. Converting the linked list to an array and then building a BST takes O(n) time. The fast–slow pointer approach repeatedly scans for the middle node, leading to O(n log n) time with O(log n) recursion stack space.
Is Convert Sorted List to Binary Search Tree asked at Google, Amazon, or Meta?
Convert Sorted List to Binary Search Tree appears in many technical interview prep lists and has been reported in interviews at companies like Amazon, Google, and Meta. The problem tests linked list traversal, recursion, and understanding of balanced binary search trees.

Ready to solve this problem?

Practice Convert Sorted List to Binary Search Tree with our built-in code editor and test cases.

Practice on FleetCode