Java 在二叉树的最大和路径的解中查找错误

Java 在二叉树的最大和路径的解中查找错误,java,dynamic,binary-tree,Java,Dynamic,Binary Tree,问题是: 给定一棵二叉树,求最大路径和 路径可以从树中的任何节点开始和结束 示例: 给定二叉树: 1 / \ 2 3 返回6 我最初的解决方案是:通过LeetCode上的90/92测试用例 public class Solution { int max = Integer.MIN_VALUE;//This is to handle the scenario where the value of all nodes is negative public int

问题是:

给定一棵二叉树,求最大路径和

路径可以从树中的任何节点开始和结束

示例:

给定二叉树:

   1
  / \
 2   3
返回6

我最初的解决方案是:通过LeetCode上的90/92测试用例

public class Solution {

    int max = Integer.MIN_VALUE;//This is to handle the scenario where the value of all nodes is negative

    public int maxPathSum(TreeNode a) {

        if(a == null){
            return 0;
        }

        int sum = maxSum(a);

        return max > sum ? max : sum;
    }

    public int maxSum(TreeNode node){

        if(node == null){
            return 0;
        }

        //handling the scenario where sum of any path is not greater than the value of single node
        if(node.val > max){
            max = node.val;
        }

        int leftChildSum = maxSum(node.left);

        //path from current node to left child is maximum
        if(node.val + leftChildSum > max){
            max = node.val + leftChildSum;
        }

        int rightChildSum = maxSum(node.right);

        //path from current node to right child is maximum
        if(node.val + rightChildSum > max){
            max = node.val + rightChildSum;
        }

        ////path from left child to right child via current node is maximum
        if(node.val + leftChildSum + rightChildSum > max){
            max = node.val + leftChildSum + rightChildSum;
        }

        return Math.max(node.val + leftChildSum, node.val + rightChildSum);
    }
}
public class Solution {

    int max = Integer.MIN_VALUE;//This is to handle the scenario where the value of all nodes is negative

    public int maxPathSum(TreeNode a) {

        if(a == null){
            return 0;
        }

        int sum = maxSum(a);

        return max > sum ? max : sum;
    }

    public int maxSum(TreeNode node){

        if(node == null){
            return 0;
        }

        //handling the scenario where sum of any path is not greater than the value of single node
        if(node.val > max){
            max = node.val;
        }

        int leftChildSum = maxSum(node.left);

        //path from current node to left child is maximum
        if(node.val + leftChildSum > max){
            max = node.val + leftChildSum;
        }

        int rightChildSum = maxSum(node.right);

        //path from current node to right child is maximum
        if(node.val + rightChildSum > max){
            max = node.val + rightChildSum;
        }

        ////path from left child to right child via current node is maximum
        if(node.val + leftChildSum + rightChildSum > max){
            max = node.val + leftChildSum + rightChildSum;
        }

        //Changes are below
        int temp = node.val;
        int value = Math.max(temp, node.val + leftChildSum);
        value = Math.max(temp, node.val + rightChildSum);
        return value;

    }
}
但我认为应该修改此解决方案。
考虑一个节点有正值的情况,它的<代码>左撇子总数和<代码> RealStHealths<代码>都是否定的。在这种情况下,应该返回节点的值

改进的解决方案:通过LeetCode上的63/92个测试用例

public class Solution {

    int max = Integer.MIN_VALUE;//This is to handle the scenario where the value of all nodes is negative

    public int maxPathSum(TreeNode a) {

        if(a == null){
            return 0;
        }

        int sum = maxSum(a);

        return max > sum ? max : sum;
    }

    public int maxSum(TreeNode node){

        if(node == null){
            return 0;
        }

        //handling the scenario where sum of any path is not greater than the value of single node
        if(node.val > max){
            max = node.val;
        }

        int leftChildSum = maxSum(node.left);

        //path from current node to left child is maximum
        if(node.val + leftChildSum > max){
            max = node.val + leftChildSum;
        }

        int rightChildSum = maxSum(node.right);

        //path from current node to right child is maximum
        if(node.val + rightChildSum > max){
            max = node.val + rightChildSum;
        }

        ////path from left child to right child via current node is maximum
        if(node.val + leftChildSum + rightChildSum > max){
            max = node.val + leftChildSum + rightChildSum;
        }

        return Math.max(node.val + leftChildSum, node.val + rightChildSum);
    }
}
public class Solution {

    int max = Integer.MIN_VALUE;//This is to handle the scenario where the value of all nodes is negative

    public int maxPathSum(TreeNode a) {

        if(a == null){
            return 0;
        }

        int sum = maxSum(a);

        return max > sum ? max : sum;
    }

    public int maxSum(TreeNode node){

        if(node == null){
            return 0;
        }

        //handling the scenario where sum of any path is not greater than the value of single node
        if(node.val > max){
            max = node.val;
        }

        int leftChildSum = maxSum(node.left);

        //path from current node to left child is maximum
        if(node.val + leftChildSum > max){
            max = node.val + leftChildSum;
        }

        int rightChildSum = maxSum(node.right);

        //path from current node to right child is maximum
        if(node.val + rightChildSum > max){
            max = node.val + rightChildSum;
        }

        ////path from left child to right child via current node is maximum
        if(node.val + leftChildSum + rightChildSum > max){
            max = node.val + leftChildSum + rightChildSum;
        }

        //Changes are below
        int temp = node.val;
        int value = Math.max(temp, node.val + leftChildSum);
        value = Math.max(temp, node.val + rightChildSum);
        return value;

    }
}

有人能帮我找出修改后的解决方案有什么问题吗?

第二个解决方案中有一个小错误:

而不是写:

int value=Math.max(temp,node.val+leftChildSum)

我应该写:

int value=Math.max(temp,node.val+leftChildSum)


第二个解决方案中有一个小错误:

而不是写:

int value=Math.max(temp,node.val+leftChildSum)

我应该写:

int value=Math.max(temp,node.val+leftChildSum)


不确定,但这应该属于codereview吗?@1Blastone不。代码没有按预期工作,OP请求帮助找出错误所在;这是离题的。不确定,但这应该属于codereview吗?@1Blastone不。代码没有按预期工作,OP请求帮助找出错误所在;那是离题的。