Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在二叉树中求和的路由数_Java_Algorithm - Fatal编程技术网

Java 在二叉树中求和的路由数

Java 在二叉树中求和的路由数,java,algorithm,Java,Algorithm,给您一个二叉树,其中每个节点都包含一个整数值 求和为给定值的路径数 路径不需要从根节点或叶节点开始或结束,但必须向下(仅从父节点移动到子节点) 假设答案为3,但我的答案返回4,我不知道为什么。您希望为算法定义两个案例 案例1:您正在搜索当前节点下可能存在的路径 案例2:您正在搜索包含当前节点的路径 您可以定义pathSum()以遵循案例1,并定义helper()以遵循案例2。这样,您可以使用pathSum()遍历树,并在任何节点上调用helper(),检查从该节点开始的有效路径 public

给您一个二叉树,其中每个节点都包含一个整数值

求和为给定值的路径数

路径不需要从根节点或叶节点开始或结束,但必须向下(仅从父节点移动到子节点)


假设答案为3,但我的答案返回4,我不知道为什么。

您希望为算法定义两个案例

  • 案例1:您正在搜索当前节点下可能存在的路径
  • 案例2:您正在搜索包含当前节点的路径
您可以定义
pathSum()
以遵循案例1,并定义
helper()
以遵循案例2。这样,您可以使用
pathSum()
遍历树,并在任何节点上调用
helper()
,检查从该节点开始的有效路径

public int pathSum(TreeNode root, int sum) {
    if (root == null) return 0;
    return pathSum(root.left, sum) + pathSum(root.right, sum) //Case 1: check for sum below current node 

    + helper(root, sum); //Case 2: check for sum starting from current node
}
public int helper(TreeNode root, int sum) {
    if (root == null) return 0;
    sum -= root.val; 
    if (sum == 0) {
        return 1; //if sum equals 0, we have a complete path, no need to go further
    }

    //else we do not have a complete path, continue searching in left and right child nodes
    return helper(root.left, sum) + pathSum(root.right, sum);
}
class Solution {
    int count = 0;
    public int pathSum(TreeNode root, int sum) {
        if (root == null) return 0;
        return helper(root, sum) + helper(root.left, sum) + helper(root.right, sum);
    }
    public int helper(TreeNode root,int sum) {
        if(root == null) {
            return 0;
        } else {
            sum -= root.val;
            if(sum == 0) {
                return 1 + pathSum(root.left,sum) + pathSum(root.right,sum);
            } else {
                return pathSum(root.left,sum) + pathSum(root.right,sum);
            }
        }        
    }
}
public int pathSum(TreeNode root, int sum) {
    if (root == null) return 0;
    return pathSum(root.left, sum) + pathSum(root.right, sum) //Case 1: check for sum below current node 

    + helper(root, sum); //Case 2: check for sum starting from current node
}
public int helper(TreeNode root, int sum) {
    if (root == null) return 0;
    sum -= root.val; 
    if (sum == 0) {
        return 1; //if sum equals 0, we have a complete path, no need to go further
    }

    //else we do not have a complete path, continue searching in left and right child nodes
    return helper(root.left, sum) + pathSum(root.right, sum);
}