Why is this c++ approach giving TLE


#1
/**
  • Definition for binary tree
  • struct TreeNode {
  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    /
    TreeNode
    new_node(int x){
    TreeNode* node=(TreeNode*)malloc(sizeof(TreeNode));
    node->val=x;
    node->left=node->right=NULL;
    return node;
    }
    TreeNode* fun(vector arr, int s, int e){
    if(s>e)
    return NULL;
    int mid = s+(e-s)/2;
    TreeNode* root=new_node(arr[mid]);
    if(s==e)
    return root;
    root->left=fun(arr,s,mid-1);
    root->right=fun(arr,mid+1,e);
    return root;
    }
    TreeNode* Solution::sortedArrayToBST(const vector &A) {
    TreeNode* root=fun(A,0,A.size()-1);
    return root;
    }

#2

Pass const vector & A to fun which means to pass as a reference rather than copying each value


#3

Try making a global copy and using it instead of passing arr into fun…


#4

thanks brother from bottom of my heart , u saved my 0.000000000000000000000006 hours


#5

or bol mat dena ki me kuch ni kar rha aage he tu muzse


#6

BTW Welcome…
Bhai mein bahut pichu hu tere se… Ab dekh le rank list…
Pata nhi ky hoga…:sleepy::sleepy: