# LC 108. Convert Sorted Array to Binary Search Tree ### [Problem link](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) ###### tags: `leedcode` `python` `c++` `easy` `BST` Given an integer array <code>nums</code> where the elements are sorted in **ascending order** , convert it to a **height-balanced** binary search tree. **Example 1:** <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" /> ``` Input: nums = [-10,-3,0,5,9] Output: [0,-3,9,-10,null,5] Explanation: [0,-10,5,null,-3,null,9] is also accepted: ``` <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" /> **Example 2:** <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" /> ``` Input: nums = [1,3] Output: [3,1] Explanation: [1,null,3] and [3,1] are both height-balanced BSTs. ``` **Constraints:** - <code>1 <= nums.length <= 10<sup>4</sup></code> - <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code> - <code>nums</code> is sorted in a **strictly increasing** order. ## Solution 1 - BST #### Python ```python= # 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 not nums: return None mid = len(nums) // 2 root = TreeNode(val=nums[mid]) root.left = self.sortedArrayToBST(nums[:mid]) root.right = self.sortedArrayToBST(nums[mid + 1:]) return root ``` #### C++ ```cpp= class Solution { public: TreeNode *traversal(vector<int> &nums, int left, int right) { if (left > right) { return nullptr; } int mid = left + ((right - left) / 2); TreeNode *cur = new TreeNode(nums[mid]); cur->left = traversal(nums, left, mid - 1); cur->right = traversal(nums, mid + 1, right); return cur; } TreeNode* sortedArrayToBST(vector<int>& nums) { TreeNode *root = traversal(nums, 0, nums.size() - 1); return root; } }; ``` >### Complexity >| | Time Complexity | Space Complexity | >| ----------- | --------------- | ---------------- | >| Solution 1 | O(n) | O(1) | ## Note follow up: [LC 1382. Balance a Binary Search Tree](https://hackmd.io/@Alone0506/Sk6V3_k3n)