![](https://i-blog.csdnimg.cn/blog_migrate/3a29d8b2f3fa758aa238518b4b23bffe.png)
解题思路1:
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @param sum int整型
* @return int整型
*/
public int count = 0;
public int FindPath (TreeNode root, int sum) {
// write code here
if(root == null){
return count;
}
method(root, sum);
//从左树重新计算
FindPath(root.left, sum);
//从右数重新计算
FindPath(root.right, sum);
return count;
}
//求和判断是否满足sum
public void method(TreeNode root, int sum){
if(root == null){
return ;
}
sum-=root.val;
if(sum == 0){
count++;
}
//计算左数
method(root.left, sum);
//计算右数
method(root.right, sum);
}
}