Leetcode 437 Solution
This article provides solution to leetcode question 437 (path-sum-iii)
Access this page by simply typing in "lcs 437" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
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);
}
};