75-days-dsa-challenge

Ninja technique🥷 to ACE DSA Interviews.

View the Project on GitHub

108. Convert Sorted Array to Binary Search Tree

Solution

Code

class Solution {
private:
    TreeNode* helper(vector<int>&nums, int l, int r){
        if(l>r) return NULL;
        int mid = l + (r-l)/2;
        TreeNode* root = new TreeNode(nums[mid]);
        root->left = helper(nums, l , mid-1);
        root->right = helper(nums, mid+1, r);
        return root;
    }
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        return helper(nums, 0 , nums.size()-1);
    }
};