LeetCode – Binary Tree Maximum Path Sum (Java)

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example, given the below binary tree

       1
      / \
     2   3

the result is 6.

Analysis

1) Recursively solve this problem
2) Get largest left sum and right sum
2) Compare to the stored maximum

Java Solution

We can also use an array to store value for recursive methods.

public int maxPathSum(TreeNode root) {
	int max[] = new int[1]; 
	max[0] = Integer.MIN_VALUE;
	calculateSum(root, max);
	return max[0];
}
 
public int calculateSum(TreeNode root, int[] max) {
	if (root == null)
		return 0;
 
	int left = calculateSum(root.left, max);
	int right = calculateSum(root.right, max);
 
	int current = Math.max(root.val, Math.max(root.val + left, root.val + right));
 
	max[0] = Math.max(max[0], Math.max(current, left + root.val + right));
 
	return current;
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Yesh

    even with your given tree, 32 is the correct answer. I fail to see how 42 is the correct answer.

  • Janardhan

    we can achieve the same without the array[] input

    private static int maxPathSum(TreeNode node, int maxSum) {

    if(node == null)

    return 0;

    int leftSum = maxPathSum(node.left, maxSum);

    int rightSum = maxPathSum(node.right, maxSum);

    int max = Math.max(node.val, Math.max(node.val+leftSum, node.val+rightSum));

    int maxTemp = Math.max(maxSum, Math.max(max,node.val+leftSum+ rightSum));

    return maxTemp;

    }

  • Janardhan

    i think we cna achieve the same without array as input

    private static int maxPathSum(TreeNode node, int maxSum) {

    if(node == null)

    return 0;

    int leftSum = maxPathSum(node.left, maxSum);

    int rightSum = maxPathSum(node.right, maxSum);

    int max = Math.max(node.val, Math.max(node.val+leftSum, node.val+rightSum));

    int maxTemp = Math.max(maxSum, Math.max(max,node.val+leftSum+ rightSum));

    return maxTemp;

    }

  • Ankit Shah

    Because of the recursion, if you pass in max instead of max[], given java pass by value, it will still store the result Integer.MIN_VALUE to the new call stack.

  • Holden

    Your code fails for this tree:
    Node root = new Node(10);
    root.left = new Node(2);
    root.right = new Node(10);
    root.left.left = new Node(20);
    root.left.right = new Node(1);
    root.right.right = new Node(-25);
    root.right.right.left = new Node(3);
    root.right.right.right = new Node(4);

    It should return 42, but returns 32 instead.

  • Holden

    There is only one solution …

  • Nitesh Kumar

    I do think the same

  • Lean Lee

    why we need use an array max[], can we use and static int variable to instead?

  • Dhaval Dave

    understand why such recurstion lsum, rsum and result is passed with pointer here at with same code and same example : http://goo.gl/wuatmP

  • ryanlr

    Not timeout from my side.

  • sam

    I get 53 even with the first solution

  • aman

    thanks, nice and easy.

  • CopperCash

    Got it. I didn’t consider negative situation

  • CopperCash

    Why don’t just find the max sum on left and right, then return (root.value + left_max + right_max)?

  • rohit_90

    First solution will work only for positive numbers while second will take care of both(+ve and -ve numbers)

  • Igal Israilevich

    Great solution! But It looks that it can be more elegant

    public int getMax(){
    return getMax(root);
    }

    private int getMax(Node x){
    if(x == null){
    return 0;
    }
    int left = getMax(x.left);
    int right = getMax(x.right);
    return x.value + (left > right ? left:right);
    }

  • czh

    These two solutions are timeout on OJ. How to solve it?

  • Kevin Lee

    Shouldn’t the last line of the first solution be “return node.value + left + right” rather than “return node.value + Math.max(left, right)”?

  • Vivek

    First solution seems to be giving wrong answer.

    TreeNode root = new TreeNode(10);
    TreeNode left1 = new TreeNode(5);
    TreeNode left2 = new TreeNode(3);
    TreeNode right1 = new TreeNode(15);
    TreeNode right2 = new TreeNode(20);

    root.left = left1;
    left1.left = left2;
    root.right = right1;
    right1.right = right2;

    Answer should be 53 but it is coming out to be 45. Second solution’s output for the same input is 53.

  • Thank you for
    sharing this and I think I need them, the information
    has been looking for.

  • Shiya

    Best solution I’ve seen so far, thank you!