How did it not get TLE?


#1

/**

  • Definition for binary tree
  • struct TreeNode {
  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    */
    #include <bits/stdc++.h>

void path(TreeNode* A,vector r,vector<vector> &res)
{
if(A == NULL) return;
if(A->right == NULL && A->left==NULL)
{
r.push_back(A->val);
res.push_back®;
return;
}
r.push_back(A->val);

if(A->right) path(A->right,r,res);

if(A->left) path(A->left,r,res);

return;

}

vector<vector > Solution::pathSum(TreeNode* A, int B) {
vector r;
vector<vector> res;
path(A,r,res);
vector<vector> ans;
for(int i = 0;i<res.size();i++)
{
if(accumulate(res[i].begin(),res[i].end(),0) == B)
ans.push_back(res[i]);
}
return ans;

}