22 · Tree Recursion

#437 Path Sum III

Mediumsolve on LeetCode ↗

frequently reported atAmazonGoogleMicrosoftMeta

the problem

In plain words

Count the paths whose values add up to targetSum. Here a path may start at any node and end at any node below it, but it must go downwards only (parent to child). It doesn't have to start at the root or end at a leaf.

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

out 3

5 → 3, 5 → 2 → 1 and -3 → 11.

in root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22

out 3

5 → 4 → 11 → 2, 5 → 8 → 4 → 5 and 4 → 11 → 7.

  • 0 ≤ number of nodes ≤ 1000
  • -10⁹ ≤ Node.val ≤ 10⁹
  • -1000 ≤ targetSum ≤ 1000

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #560
      Subarray Sum Equals K
      Mediumin Hashing
    2. #112
      Path Sum
      Easyin Tree Recursion
    3. #113
      Path Sum II
      Mediumin Tree Recursion