You is given a binary tree in which each node contains an integer value.
Find the number of paths this sum to a given value.
The path does not need-to-start or end at the root or a leaf, but it must go downwards (traveling with from parent nodes T o child nodes).
The tree has no more than nodes and the values is in the range-1,000,000 to 1,000,000.
Example:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 / 5 -3 /\ 3 2 one /\ 3 - 2 1Return 3. The paths, sum to 8 are:1. 5. 5, 2, 13. -3-11
Runtime:39ms
1 /**2 * Definition for a binary tree node.3 * struct TreeNode {4 * int val;5 * TreeNode *left;6 * TreeNode *right;7 * TreeNode (int x): Val (x), left (null), right (null) {}8 * };9 */Ten classSolution { One Public: A intPathsum (treenode* root,intsum) { - if(!root)return 0; - returnHelper (root, sum) + pathsum (root->left, sum) + pathsum (root->Right , sum); the } - - intHelper (treenode* root,intsum) { - if(!root)return 0; + intCount = Root->val = = sum?1:0; - returnCount + helper (Root->left, sum-root->val) + +Helper (Root->right, sum-root->val); A } at};
Path Sum III