C solution,very easy to understand,6 lines

  • 0
    int helper(struct TreeNode* root, int target) {
          int ret=0;
          if(root==NULL) return ret;
          if(target==root->val)  ret++;
          return ret+helper(root->left,target-root->val)+helper(root->right,target-root->val);
    int pathSum(struct TreeNode* root, int sum) {
         if(root==NULL) return 0;
         return helper(root,sum)+pathSum(root->left,sum)+pathSum(root->right,sum);

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.