Best O(N) Solution
Approach\nOptimal\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n\n/**\n * Definition for a binary tree node.\n * struct Tree
# Approach\nOptimal\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    TreeNode* build(int start, int end, vector<int>& nums) {\n        if (start > end)\n            return NULL;\n        int ind = (start + end) / 2;\n        TreeNode* root = new TreeNode(nums[ind]);\n        root->left = build(start, ind-1, nums);\n        root->right = build(ind+1, end, nums);\n\n        return root;    \n    }\n\n    TreeNode* sortedArrayToBST(vector<int>& nums) {\n        return build(0, nums.size() - 1, nums);\n    }\n};\n```
['C++']