My DFS java Solution


  • 0
    J
    public class Solution {
        int max = Integer.MIN_VALUE;
        
        public int maxPathSum(TreeNode root) {
            helper(root);
            return max;
        }
        
        private int helper(TreeNode root){
            if(root == null) return 0;
            
            int left = helper(root.left);
            int right = helper(root.right);
            
            int temp = Math.max(Math.max(left + root.val,right + root.val), root.val);
            
            max = Math.max(Math.max(temp, max), left + right + root.val);
            
            return temp;
        }
    }
    

Log in to reply
 

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