执行结果:
通过
显示详情
执行用时 :16 ms, 在所有 Java 提交中击败了70.04% 的用户
内存消耗 :41.3 MB, 在所有 Java 提交中击败了12.40%的用户
题目:
给定一个二叉树,它的每个结点都存放着一个整数值。
找出路径和等于给定数值的路径总数。
路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。
二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数。
示例:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
10
/ \
5 -3
/ \ \
3 2 11
/ \ \
3 -2 1
返回 3。和等于 8 的路径有:
1. 5 -> 3
2. 5 -> 2 -> 1
3. -3 -> 11
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/path-sum-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路:
双递归,把所有节点当作头,然后对于每一个头在进行递归。
代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int pathSum(TreeNode root, int sum) {
if(root==null)
{
return 0;
}
return help(root,sum)+pathSum(root.left,sum)+pathSum(root.right,sum);
}
public int help(TreeNode root,int sum)
{
if(root==null)
{
return 0;
}
int count=0;
if(root.val==sum)
{
count++;
}
count+=help(root.left,sum-root.val);
count+=help(root.right,sum-root.val);
return count;
}
}