Untitled
unknown
java
8 months ago
531 B
2
Indexable
public class Solution { private int maxSum = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { maxGain(root); return maxSum; } private int maxGain(TreeNode node) { if (node == null) return 0; int leftGain = Math.max(maxGain(node.left), 0); int rightGain = Math.max(maxGain(node.right), 0); int priceNewPath = node.val + leftGain + rightGain; maxSum = Math.max(maxSum, priceNewPath); return node.val + Math.max(leftGain, rightGain); } }
Editor is loading...
Leave a Comment