/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int pathSum(TreeNode root, int sum) {
if (root == null) return 0;
return countPath(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
}
private int countPath(TreeNode root, int sum) {
if (root == null) return 0;
int count = 0;
if (root.val == sum) count++;
count += countPath(root.left, sum - root.val);
count += countPath(root.right, sum - root.val);
return count;
}
}