Maximum Sum Path Between Any Two Nodes of a Weighted Tree
Last updated: November 29, 2026
Quick Overview
Each node in a binary tree has an integer weight that may be negative. Return the largest sum over all paths, where a path is any sequence of adjacent nodes with no repeats and contains at least one node.
Meta
November 29, 20260
0
0 solved
Each node in a binary tree has an integer weight that may be negative. Return the largest sum over all paths, where a path is any sequence of adjacent nodes with no repeats and contains at least one node.
Binary tree maximum path sum is a classic Meta onsite Hard. The interviewer checks that you separate the value a node returns to its parent (one branch) from the value it can contribute as a path apex (both branches), and that negative branches are dropped.
What the Interviewer Expects
- Distinguishes the returned single-branch gain from the apex sum
- Clamps negative child gains to zero
- Initializes the answer so all-negative trees work
- Uses one post-order traversal
- Discusses recursion depth for skewed trees
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the path itself, not just the sum.
- Restrict paths to go from a leaf to a leaf.
- Solve it for a general tree with any number of children.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Up to 3 * 10^4 nodes, weights between -1000 and 1000. Path must have at least one node and need not include the root or a leaf. Output an integer....
High-Level Design
Define gain(node) as the best sum of a downward path starting at node and going into at most one child. Then gain(node) = node.val + max(0, gain(left), gain(right)). A path whose highest node is `...