Leetcode 437 Solution

This article provides solution to leetcode question 437 (path-sum-iii)

https://leetcode.com/problems/path-sum-iii

Solution

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int pathSum(TreeNode* root, int sum, int curr)
    {
        if (root == NULL)
        {
            return 0;
        }

        curr += root->val;

        return (sum == curr) + pathSum(root->left, sum, curr) + pathSum(root->right, sum, curr);
    }

    int pathSum(TreeNode* root, int sum) {
        if (root == NULL)
            return 0;

        return pathSum(root, sum, 0) + pathSum(root->left, sum) + pathSum(root->right, sum);
    }
};