Given the root
of a binary tree and an integer targetSum
, return the number of paths where the sum of the values along the path equals targetSum
.
The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).
Example 1:
Input: root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8 Output: 3 Explanation: The paths that sum to 8 are shown.
Example 2:
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 Output: 3
Constraints:
[0, 1000]
.-10^9 <= Node.val <= 10^9
-1000 <= targetSum <= 1000
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
小心数据溢出
- class Solution {
- public:
- int pathSum(TreeNode* root, int targetSum) {
- if (root == nullptr) {return 0;}
- return pathSumStartWithRoot(root, targetSum)
- + pathSum(root->left, targetSum)
- + pathSum(root->right, targetSum);
- }
-
- int pathSumStartWithRoot(TreeNode* root, int sum) {
- if (!root) {return 0;}
- int count = root->val == sum? 1: 0;
- if (double(sum) - double(root->val) < INT_MIN) {return 0;}
- int remaingSum = sum - root->val;
- count += pathSumStartWithRoot(root->left, remaingSum);
- count += pathSumStartWithRoot(root->right, remaingSum);
- return count;
- }
- };
- class Solution {
- public:
- int pathSum(TreeNode* root, int targetSum) {
- int pathCount = 0;
- vector<int> currentPath;
- pathSumHelper(root, targetSum, currentPath, pathCount);
- return pathCount;
- }
-
- void pathSumHelper(TreeNode *root,
- int sum ,
- vector<int> ¤tPath,
- int &pathCount) {
- if(!root) return;
- currentPath.push_back(root->val);
- double currentSum = 0;
- for(int j=currentPath.size()-1; j>=0; j--) {
- currentSum += (double)currentPath[j];
- if(currentSum == sum) pathCount++;
- }
- pathSumHelper(root->left, sum, currentPath, pathCount);
- pathSumHelper(root->right, sum, currentPath, pathCount);
- currentPath.pop_back();
- }
- };
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode() {}
- * TreeNode(int val) { this.val = val; }
- * TreeNode(int val, TreeNode left, TreeNode right) {
- * this.val = val;
- * this.left = left;
- * this.right = right;
- * }
- * }
- */
- class Solution {
- public int pathSum(TreeNode root, int targetSum) {
- if (root == null) {return 0;}
- return pathSumHelper(root, targetSum)
- + pathSum(root.left, targetSum)
- + pathSum(root.right, targetSum);
- }
-
- public int pathSumHelper(TreeNode root, int sum) {
- if (root == null) {return 0;}
- int count = sum == root.val ? 1 : 0;
- if (Double.valueOf(sum)
- - Double.valueOf(root.val)
- < Integer.MIN_VALUE) {
- return 0;
- }
- int remaining = sum - root.val;
- return count
- + pathSumHelper(root.left, remaining)
- + pathSumHelper(root.right, remaining);
- }
- }