When there’s incursion, the solution is really easy ;)

class Solution {
public:
    TreeNode *sortedArrayToBST(vector<int> &num) {
        return buildTree(num,0,num.size());
    }
    
    TreeNode *buildTree(vector<int> &num, int l, int r){
        if(l >= r) return NULL;
        
        int m = (l + r) / 2;
        TreeNode* head = (TreeNode*)malloc(sizeof(TreeNode));
        head->val = num[m];
        
        head->left = buildTree(num, l,m);
        head->right = buildTree(num, m+1,r);
        return head;
    }
};