题解 | #二叉树中和为某一值的路径(三)#
二叉树中和为某一值的路径(三)
http://www.nowcoder.com/practice/965fef32cae14a17a8e86c76ffe3131f
import java.util.*;
public class Solution {
public int sum(TreeNode root, int sum){
if(root == null)return 0;
int temp=0;
if(root.val == sum)temp++;
return temp+sum(root.left, sum - root.val)+sum(root.right, sum - root.val);
}
public int FindPath (TreeNode root, int sum) {
// write code here
int ans = 0;
if(root == null)return 0;
ans += sum(root,sum);
ans += FindPath(root.left, sum);
ans += FindPath(root.right, sum);
return ans;
}
}