Tree == Recursion


#1
  • Definition for binary tree

  • struct TreeNode {

  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    /
    vector<vector > Solution::pathSum(TreeNode
    A, int B) {
    vector<vector > sol;
    int flag=0;
    if(A->left)
    {flag=1;
    vector<vector > soll=pathSum((A->left),B-(A->val));
    for(int i=0;i<soll.size();i++)
    {
    soll[i].insert(soll[i].begin(),A->val);
    sol.push_back(soll[i]);
    }

    }
    if(A->right)
    {flag=1;
    vector<vector > solr=pathSum((A->right),B-(A->val));
    for(int i=0;i<solr.size();i++)
    {
    solr[i].insert(solr[i].begin(),A->val);
    sol.push_back(solr[i]);
    }
    }
    if(flag==0)
    {
    if(B-A->val==0)
    {
    vectortt;
    tt.push_back(A->val);
    sol.push_back(tt);
    return sol;
    }

    }
    return sol;

}