LeetCode 108 - Convert Sorted Array to Binary Search Tree Problem

Problem Statement

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.


Given the sorted array: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

     / \
   -3   9
   /   /
 -10  5


# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def sortedArrayToBST(self, nums: List[int]) -> TreeNode: if not nums: return None return self._createBST(nums, 0, len(nums)-1) def _createBST(self, nums, low, high): if low>high: return None mid = low+(high-low)//2 node = TreeNode(nums[mid]) node.left = self._createBST(nums, low, mid-1) node.right = self._createBST(nums, mid+1, high) return node