As reported: Cohesity MTS-3, May 2025, round 1 (DSA, one hour): Binary Tree Maximum Path Sum, followed in the last 10 minutes by a second tree problem (maximum sum of non-adjacent nodes).
This is LeetCode 124 — Binary Tree Maximum Path Sum. Our write-up: /binary-tree-maximum-path-sum.
A path in a binary tree is a sequence of nodes where each adjacent pair is connected by an edge, and a node appears at most once. A path does not need to pass through the root, and it contains at least one node. The path sum is the sum of its nodes' values.
Given the root, return the maximum path sum of any non-empty path.
Example 1
root = [1,2,3]
Output: 6
The path 2 → 1 → 3.
Example 2
root = [-10,9,20,null,null,15,7]
Output: 42
The path 15 → 20 → 7.
Constraints: the number of nodes is in [1, 3 * 10^4], -1000 <= Node.val <= 1000.
Code runner format: one JSON value per line: the tree in LeetCode's level-order format. Implement maxPathSum(root) returning an integer.