Find the Highest Scoring Path Anywhere in a Tree
Last updated: November 29, 2026
Quick Overview
Each node in a binary tree of Azure service dependencies has an integer health score that can be negative. Return the largest sum along any path between two nodes, where a path follows parent and child edges and visits each node at most once.
Microsoft
November 29, 20260
0
0 solved
Each node in a binary tree of Azure service dependencies has an integer health score that can be negative. Return the largest sum along any path between two nodes, where a path follows parent and child edges and visits each node at most once.
A well-known hard tree problem that Microsoft onsite interviewers use to test whether a candidate can separate what a recursive call returns from what it records. Negative values are the trap.
What the Interviewer Expects
- Distinguish a path that bends at a node from one that continues to the parent
- Return only a one-sided gain to the parent
- Clamp negative child gains to zero
- Track a global best that starts at negative infinity
- Handle all-negative 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 actual node sequence of the best path.
- Restrict paths to go from a leaf to a leaf.
- Do it without recursion.
- What if the graph is 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
- Input: root of a non-empty binary tree with integer values.
- A path is any sequence of connected nodes, at least one node, no repeats.
- Output: maximum path sum....
High-Level Design
At each node, there are two different quantities. The best path that bends here uses both children: val + left_gain + right_gain, a candidate for the global answer. But the parent can only extend a ...