Skip to content

Latest commit

 

History

History
18 lines (17 loc) · 556 Bytes

27.md

File metadata and controls

18 lines (17 loc) · 556 Bytes

Convert Sorted Array To BST

LeetCode Link

    TreeNode* BST(vector<int>&nums,int left, int right){
        if(left>right){
            return nullptr;
        }
        int mid=left+(right-left)/2;
        TreeNode* node=new TreeNode(nums[mid]);
        node->left=BST(nums,left,mid-1);
        node->right=BST(nums,mid+1,right);
        
        return node;
    }
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        return BST(nums,0,nums.size()-1);
    }