java - 在二叉树中最大和路径的解决方案中查找错误

标签 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 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);
    }
}

但我认为这个解决方案应该修改。
考虑一个场景,其中节点具有正值,并且其 leftChildSumrightChildSum 均为负值。在这种情况下,应该返回节点的值。

修改后的解决方案:在 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;
        }

        //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);

value = Math.max(temp, node.val + rightChildSum);

我应该写:

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

value = Math.max(value, node.val + rightChildSum);

关于java - 在二叉树中最大和路径的解决方案中查找错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41509915/

相关文章:

java - JProgress Bar 未在 SwingWorker 内更新

java - 如何使 javax Transformer 输出 HTML(无自闭标签)?

actionscript - Away3D 4.0 从数组加载图元的动态纹理

jQuery - 将输入值更改为复选框并选中

algorithm - 将相同的值插入二项式堆

Java继承而不强制转换

java - Java Web 应用程序中的 Oracle 10g 数据库集成

java - 字符串到日期 24 小时格式 Java

javascript - 通过动态变量访问 Json 值

java - Java中二叉树的递归检查