Simple Java solution


  • 0
    I

    '''class Solution {

    int max;
    public int maxPathSum(TreeNode root) {
        max = Integer.MIN_VALUE;
        int n = trav(root);
        if (n>max)
            max=n;
        return max;
    }
    
    private int trav(TreeNode root)
    {
        if(root==null)
            return 0;
        int l = trav(root.left);
        int r = trav(root.right);
        int curr = root.val;
        if(l>0)
            curr+=l;
        if(r>0)
            curr+=r;
        //int a = 0;
        /*if(curr+l>curr+a)
            a+=l;
        if(curr+r>curr+a)
            a+=r;
        curr+=a;*/
        if(curr>max)
            max = curr;
        int a = 0;
        if( Math.max(l,r)>0)
            a = Math.max(l,r);
        return (root.val+a);
    
    }
    

    }
    '''


Log in to reply
 

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