FunctionRacerProblemsRace

Path Sum III

MediumTreesPython, JavaScript, TypeScript, Java, C++

You are given the root of a binary tree. Count the downward paths whose values add up to targetSum. A path may start at any node and must go from a parent to a child, repeatedly, until it stops at any node (it does not need to reach a leaf). A single node can be a path. Return the number of such paths.

Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.

Example 1

root [10,5,-3,3,2,null,11,3,-2,null,1]
targetSum 8
returns 3

The paths 5-3, 5-2-1 and -3-11 add to 8.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Trees problems