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>

  1. Shiya on 2014-6-1

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

  2. friv 5 on 2014-7-5

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

  3. Vivek on 2014-8-17

    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.

  4. Kevin Lee on 2014-9-13

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

  5. czh on 2014-11-2

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

  6. Igal Israilevich on 2014-12-10

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

  7. rohit_90 on 2015-1-3

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

  8. CopperCash on 2015-1-7

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

  9. CopperCash on 2015-1-10

    Got it. I didn’t consider negative situation

  10. aman on 2015-3-8

    thanks, nice and easy.

  11. sam on 2015-4-1

    I get 53 even with the first solution

  12. ryanlr on 2015-4-23

    Not timeout from my side.

  13. Dhaval Dave on 2015-5-11

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

  14. Lean Lee on 2015-8-20

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

  15. Nitesh Kumar on 2015-9-5

    I do think the same

  16. Holden on 2015-9-19

    There is only one solution …

  17. Holden on 2015-9-19

    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.

  18. Ankit Shah on 2016-2-22

    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.

  19. Janardhan on 2016-3-30

    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;

    }

  20. Janardhan on 2016-3-30

    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;

    }

  21. Yesh on 2016-4-13

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

  22. Arian Hs on 2017-1-7

    Can you add the snippet to output the actual path for it? e.g. in this format 3->1->2

  23. Saurabh Rane on 2017-2-10
  24. Mayank Mittal on 2017-4-2

    better solution :
    public int findMaxSum(Node root){

    Node node = root;
    return findMaxSum(node,true);
    }

    private int findMaxSum(Node node, boolean flag) {

    if(node == null){
    return 0;
    }
    if(flag){
    return node.value + findMaxSum(node.left,false) + findMaxSum(node.right, false);
    }

    return node.value + Math.max(findMaxSum(node.left,false), findMaxSum(node.right, false));
    }

Leave a comment

*