← Back to index
EasyPython2 min read

Convert Sorted Array to Binary Search Tree

build balanced bst from sorted array by recursively using middle element as root.

treedivide-and-conquer

Problem link

View on LeetCode

Solutions in this repo

  • Python../convert-sorted-array-to-binary-search-tree/solution.pyPython solution

recursively: pick middle element as root, left half becomes left subtree, right half becomes right subtree. this ensures balanced tree.

base case: empty array returns null. the middle element naturally creates a balanced structure since we split evenly.

complexity

O(n) time to visit each element once, O(log n) space for recursion stack. balanced tree height is log n.