Share my easy Java solution


  • 0
    B

    public class Solution {

    private int ans=Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        helper(root);
        return ans;
    }
    private int helper(TreeNode root){
        //base case
        if(root==null)return 0;
        
        int left=helper(root.left);
        int right=helper(root.right);
        ans=Math.max(ans,left+right+root.val);
        return (Math.max(left,right)+root.val)>0?Math.max(left,right)+root.val:0;
    }
    

    }


Log in to reply
 

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