Count Downward Tree Paths Whose Cost Deltas Sum to a Target
Last updated: November 29, 2026
Quick Overview
Each node of a binary tree holds a signed cost delta for an Azure resource hierarchy. Count the paths that go downward from any node to any descendant (or itself) whose deltas sum exactly to a target.
Microsoft
November 29, 20260
0
0 solved
Each node of a binary tree holds a signed cost delta for an Azure resource hierarchy. Count the paths that go downward from any node to any descendant (or itself) whose deltas sum exactly to a target.
This tests whether a candidate can carry the prefix-sum trick from arrays onto root-to-node paths in a tree. Microsoft interviewers look for the backtracking step that removes a prefix when the DFS leaves a subtree.
What the Interviewer Expects
- Start with the O(n^2) brute force from every node
- Treat each root-to-node path as an array and use prefix sums
- Keep a hash map of prefix sums on the current path
- Seed the map with 0 to count paths starting at the root
- Backtrack the map after visiting children
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 paths themselves, not just the count.
- Allow paths that go up and then down (through a turning node).
- Convert the recursion to an iterative traversal.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Values may be negative; paths must go parent to child.
- A path has at least one node.
- Output: number of such paths....
High-Level Design
Brute force runs a DFS from every node and is O(n^2) on skewed trees. Better: along the current root-to-node path, keep the running sum and a count of every prefix sum seen above. A path ending here s...