↓Skip to main content
  1. LeetCode/

LeetCode 108: Convert Sorted Array to Binary Search Tree

·1 min· ·
LeetCode Easy Array Divide-and-Conquer Tree Binary-Search-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-16

  • Submissions: Runtime Error ×5 → Accepted → Runtime Error ×2 → Accepted → Runtime Error → Accepted ×2

Approach
#

The array is sorted, so the middle element has smaller values on its left and larger on its right, making it the root (Tree Template 3). Recurse on both halves; splitting at the middle every time keeps their sizes within one, so the height is balanced. Pass index ranges to avoid copying slices.

My Attempts
#

Several early versions sliced the right half as nums[mid:], including the middle element, so a single element kept producing the same slice and recursed forever (RecursionError); others read attributes of None.

Solution
#

class Solution:
    def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
        def build(lo, hi):
            if lo > hi:
                return None
            mid = (lo + hi) // 2
            return TreeNode(nums[mid], build(lo, mid - 1), build(mid + 1, hi))
        return build(0, len(nums) - 1)

Complexity
#

Time O(n), space O(log n).

Mistakes to Avoid
#

  • Exclude mid from the recursive ranges, or the recursion never ends.

Interview Explanation
#

The sorted array’s middle element is the root and each half builds a subtree; the sizes differ by at most one, so it is balanced. With index ranges, time O(n).

Related

LeetCode 110: Balanced Binary Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Tree
LeetCode 110 note: every node’s subtree heights must differ by at most 1; return heights in post-order and propagate -1 once unbalanced
LeetCode 111: Minimum Depth of Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 111 note: minimum depth must end at a leaf; with only one child, the empty side (depth 0) cannot count as the minimum
LeetCode 112: Path Sum
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 112 note: subtract each value from targetSum on the way down; it works if the remainder matches at a leaf
LeetCode 119: Pascal's Triangle II
·2 mins
LeetCode Easy Array Dynamic-Programming
LeetCode 119 note: only row rowIndex is needed; update a 1D array from right to left for O(k) space
LeetCode 100: Same Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 100 note: two empty trees are equal, one empty and one not differ, and otherwise compare values and recurse on both sides
LeetCode 101: Symmetric Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 101 note: the left and right subtrees must mirror each other, outer with outer and inner with inner; compare values, not node objects