My first instinct was to treat it like the binary tree diameter problem, where you track the best single-arm extension you can pass upward and combine two arms at each node.
Use a post-order DFS that returns the maximum downward path sum from each node. At each node, combine the top two child contributions to compute the best path through that node, and maintain a global maximum.
Pro tip: Explicitly handle negative values by allowing paths to start and end at the same node, and mention that the algorithm runs in O(n) time and O(h) space, which is optimal for this problem.
Confirm that a path can be a single node, that node values can be negative, and that the path cannot revisit nodes. This ensures you handle edge cases correctly.
Design a DFS function that returns the maximum sum of a downward path starting at the current node. This function will be used to compute contributions from children.
At each node, collect the positive contributions from its children, sort them, and take the top two. The sum of the node's value and these top two contributions gives the best path passing through the node.
Compare the local maximum with a global variable and update it if larger. This global variable will hold the final answer.
Return the node's value plus the maximum child contribution (or 0 if all are negative) to its parent. This allows the parent to compute its own local maximum.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.