My AC java recursive solution


  • 8
    S

    max1 is the max value of the current node to pass to the upper level node.

    max is the global max value that could be max1 or the sum of root and left max and right max

    public class Solution {
    
    int max=Integer.MIN_VALUE;
    
    public int maxPathSum(TreeNode root) {
        maxPathSumR(root);
        return max;
    }
    
    public int maxPathSumR(TreeNode root) {
        
        if(root==null) return 0;
        int left=maxPathSumR(root.left);
        int right=maxPathSumR(root.right);
        
        int max1 = Math.max(root.val,Math.max(root.val+left,root.val+right));
        max = Math.max(max,Math.max(max1,left+right+root.val));
        return max1;
    }
    

    }


  • 0
    J
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.