Dsa
Trees
Medium
Path Sum with Value Count
Given a binary tree and an integer value, determine the number of root-to-leaf paths where each path's sum equals the given value. A root-to-leaf path is defined as a path from the root node to any leaf node.
Input format:
- The first line contains an integer n, the number of nodes in the binary tree.
- The next n lines each contain three integers indicating the parent node's value, the left child's value (or -1 if no left child), and the right child's value (or -1 if no right child).
- The last line contains a single integer value, the sum to check against.
Output format:
- Output the number of root-to-leaf paths that equal the given sum.
Example:
Input:
5
1 2 3
2 4 -1
2 5 -1
3 -1 -1
4 -1 -1
7
Output:
2
Key concepts
treesdepth-first searchrecursion
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now