leetcode-437-路径总和III

class Solution {

public:

    //包含root结点的条数

    int findPath(TreeNode* root, int sum){

        int res = 0;

        if (root){

            if (root->val == sum) res++;

           // if (root->left == NULL && root->right == NULL) return res;

            res += findPath(root->left, sum-root->val);

            res += findPath(root->right, sum-root->val);

        }

        return res;

    }

 

    int pathSum(TreeNode* root, int sum) {

        int res =0;

        if (root){

            res = findPath(root, sum) ;

            res += pathSum(root->left, sum);

            res += pathSum(root->right, sum);

        }

        return res;

    }

};

相关文章:

  • 2022-12-23
  • 2021-07-26
  • 2022-12-23
  • 2021-06-12
  • 2021-11-05
  • 2022-12-23
  • 2021-10-07
  • 2022-12-23
猜你喜欢
  • 2022-02-10
  • 2021-11-24
  • 2021-11-01
  • 2021-08-28
  • 2021-09-02
  • 2022-12-23
相关资源
相似解决方案