22 · Tree Recursion

#112 Path Sum

Easysolve on LeetCode ↗

frequently reported atMicrosoftAmazonBloombergMeta

the problem

In plain words

Given a binary tree and a number targetSum, return true if some path from the root down to a leaf has values adding up to exactly targetSum. A leaf is a node with no children. An empty tree has no paths.

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

out true

5 + 4 + 11 + 2 = 22.

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

out false

The two root-to-leaf sums are 3 and 4.

in root = [], targetSum = 0

out false

No nodes, no paths.

  • 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. #113
      Path Sum II
      Mediumin Tree Recursion
    2. #129
      Sum Root to Leaf Numbers
      Mediumin Tree Recursion
    3. #437
      Path Sum III
      Mediumin Tree Recursion