22 · Tree Recursion

#113 Path Sum II

Mediumsolve on LeetCode ↗

frequently reported atBloombergMicrosoftMetaAmazon

the problem

In plain words

Return every root-to-leaf path whose values add up to targetSum. Write each path as the list of its values from the root down to the leaf. The paths may come in any order.

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

out [[5,4,11,2],[5,8,4,5]]

in root = [1,2,3], targetSum = 5

out []

in root = [1,2], targetSum = 0

out []

  • 0 ≤ number of nodes ≤ 5000
  • -1000 ≤ Node.val ≤ 1000
  • -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. #112
      Path Sum
      Easyin Tree Recursion
    2. #78
      Subsets
      Mediumin Backtracking
    3. #437
      Path Sum III
      Mediumin Tree Recursion