在二进制树中的最大求和路径的解决方案中查找错误

时间:2017-01-06 16:27:45

标签: 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;

    }
}

有人可以帮我弄清楚我修改后的解决方案有什么问题吗?

1 个答案:

答案 0 :(得分:0)

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

而不是写作:

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