Binary Tree Maximum Path Sum LeetCode solution in JavaScript
1var maxPathSum = function (root) {2let res = -Infinity;3function solve(root) {4if (!root) {5return 0;6}78let left = solve(root.left);9let right = solve(root.right);10//ignore the values with negative sum11let leftVal = Math.max(left, 0);12let rightVal = Math.max(right, 0);1314let localMax = leftVal + rightVal + root.val;15res = Math.max(localMax, res);16return Math.max(leftVal, rightVal) + root.val;17}18solve(root);19return res;20};