Count downward paths in a binary tree that add up to a target sum

Quick Overview

Count the downward paths in a binary tree whose node values add up to a target sum, where a path can start and end at any node. It tests whether you can do better than walking down from every node, and how you handle negative values, a zero target and large sums.

Count downward paths in a binary tree that add up to a target sum

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given the `root` of a binary tree and an integer `target_sum`, return the number of paths where the sum of the values along the path equals `target_sum`. The path does not need to start or end at the root or a leaf, but it must go downwards, traveling only from parent nodes to child nodes. ### Function Signature ```python def count_target_sum_paths(root: Optional[TreeNode], target_sum: int) -> int: ``` ### Rules - `TreeNode` has the fields `val`, `left` and `right`. An empty tree is passed as `root = None` and has no paths. - The examples write the tree as a level-order list, with `None` marking a missing child. - A path is a sequence of one or more nodes where each node is the child of the one before it. A single node is a valid path. - Paths are counted by the nodes they contain, so two paths over different nodes both count even when their values are the same. ### Constraints - The number of nodes in the tree is in the range `[0, 1000]`. - `-10^9 <= Node.val <= 10^9` - `-1000 <= target_sum <= 1000` ### Examples ```text Input: root = [10,5,-3,3,2,None,11,3,-2,None,1], target_sum = 8 Output: 3 ``` The paths that sum to 8 are 5 -> 3, 5 -> 2 -> 1 and -3 -> 11. None of the paths that start at the root 10 reach exactly 8. ```text Input: root = [5,4,8,11,None,13,4,7,2,None,None,5,1], target_sum = 22 Output: 3 ``` The paths that sum to 22 are 5 -> 4 -> 11 -> 2, 5 -> 8 -> 4 -> 5 and 4 -> 11 -> 7. The last one starts below the root and ends at a leaf. ```text Input: root = [0,0,0], target_sum = 0 Output: 5 ``` Each of the three nodes is a path of sum 0 on its own, and the root with either child gives two more.

Overview: Count the downward paths in a binary tree whose node values add up to a target sum, where a path can start and end at any node. It tests whether you can do better than walking down from every node, and how you handle negative values, a zero target and large sums.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given the root of a binary tree and an integer target_sum, return the number of paths where the sum of the values along the path equals target_sum.

The path does not need to start or end at the root or a leaf, but it must go downwards, traveling only from parent nodes to child nodes.

Function Signature

def count_target_sum_paths(root: Optional[TreeNode], target_sum: int) -> int:

Rules

  • TreeNode has the fields val , left and right . An empty tree is passed as root = None and has no paths.
  • The examples write the tree as a level-order list, with None marking a missing child.
  • A path is a sequence of one or more nodes where each node is the child of the one before it. A single node is a valid path.
  • Paths are counted by the nodes they contain, so two paths over different nodes both count even when their values are the same.

Constraints

  • The number of nodes in the tree is in the range [0, 1000] .
  • -10^9 <= Node.val <= 10^9
  • -1000 <= target_sum <= 1000

Examples

Input: root = [10,5,-3,3,2,None,11,3,-2,None,1], target_sum = 8
Output: 3

The paths that sum to 8 are 5 -> 3, 5 -> 2 -> 1 and -3 -> 11. None of the paths that start at the root 10 reach exactly 8.

Input: root = [5,4,8,11,None,13,4,7,2,None,None,5,1], target_sum = 22
Output: 3

The paths that sum to 22 are 5 -> 4 -> 11 -> 2, 5 -> 8 -> 4 -> 5 and 4 -> 11 -> 7. The last one starts below the root and ends at a leaf.

Input: root = [0,0,0], target_sum = 0
Output: 5

Each of the three nodes is a path of sum 0 on its own, and the root with either child gives two more.