Save 25%Black Friday: 25% off all plans — use code BLACKFRIDAY2026 at checkout. Ends Friday, Dec 4.Claim the deal

Given a binary tree in which each node holds an integer value, determine the maximum path sum that connects two leaf nodes. A leaf node is defined as a node that does not have any left or right children. The path, which must begin and end at leaf nodes, can pass through intermediate (parent) nodes. The path sum is calculated as the sum of the node values along the traversal.

Constraints:

  • The number of nodes in tree is in the range [1,105][1, 10^5].
  • −109≤Node.val≤109-10^9 \leq \text{Node.val} \leq 10^9
  • A tree must be a binary tree.
Test Cases
Input
Output
Expected