108. Convert Sorted Array to Binary Search Tree¶
Edited by Charles
Easy · Python · Binary Tree
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Recursion:3
The core idea is to just focus on creating the current node and letting the creation of the left and right children to the next recursions. The terminal condition is the input list is empty.
Original study notes follow, including intermediate snippets and earlier attempts.
Recursion:3
The core idea is to just focus on creating the current node and letting the creation of the left and right children to the next recursions. The terminal condition is the input list is empty.
The list is sorted, and thus we partition the list in every recursion by the middle 704. Binary Search .
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | January 19, 2026 |
Python solution¶
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if len(nums) == 0:
return None
idx = len(nums) // 2
root = TreeNode(val = nums[idx])
root.left = self.sortedArrayToBST(nums[:idx])
root.right = self.sortedArrayToBST(nums[idx + 1:])
return root