leetcode 113:路径总和 II

类似于排列,使用递归的方式,运行时间12ms

void path(TreeNode *root,int &sum,std::vector<std::vector<int>> &aa,std::vector<int> b){
    if(root->left==NULL&&root->right==NULL&&sum==root->val) {
        b.push_back(root->val);
        aa.push_back(b);
        //sum=sum+b.back();
        return;
    }
    else
    {
        int d=sum;
        int a=root->val;
            b.push_back(root->val);
            int sum=d-a;
            if(root->left!=NULL)
                path(root->left,sum,aa,b);
            if(root->right!=NULL)
                path(root->right,sum,aa,b);
    }
}

std::vector<std::vector<int>> pathSum(TreeNode* root, int sum) {
    std::vector<std::vector<int>> aa;
    std::vector<int> b;
    if(root==NULL)
        return aa;
    path(root,sum,aa,b);
    return aa;
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-19
  • 2022-12-23
  • 2021-08-12
  • 2021-12-20
  • 2021-10-29
  • 2021-06-14
猜你喜欢
  • 2021-04-08
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
  • 2021-07-06
  • 2021-08-01
相关资源
相似解决方案