108. Convert Sorted Array to Binary Search Tree 發表於 2023-02-13 | 分類於 leetcode problemsolution12345678910111213class Solution {public: TreeNode* sortedArrayToBST(vector<int>& nums) { if(nums.empty()) return nullptr; int n = nums.size(); TreeNode *root = new TreeNode(nums[n/2]); vector<int> left(nums.begin(), nums.begin()+n/2); vector<int> right(nums.begin()+n/2+1, nums.end()); root->right = sortedArrayToBST(right); root->left = sortedArrayToBST(left); return root; }}; analysis time complexity O(n) space complexity O(n)