C++ Easy Understand solution


  • 0
    F
    class Solution {
    public:
        int pathSum(TreeNode* root, int sum) {
            if(!root) return 0;
            return pathSum(root->left, sum) + pathSum(root->right, sum) + pathFromCurrentNode(root,sum);       
        }    
        int pathFromCurrentNode(TreeNode* t, int sum ) {
            if(t==NULL) return 0;
            return (t->val == sum) + pathFromCurrentNode(t->left,sum - t->val) +pathFromCurrentNode(t->right, sum - t->val);     
        }       
    };
    

Log in to reply
 

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