FunctionRacerProblemsRace

Path Sum II

MediumTreesPython, JavaScript, TypeScript, Java, C++

You are given the root of a binary tree. Find every root-to-leaf path whose values add up to targetSum. Return them as a list of paths, each path a list of values from the root down. List the paths in left-to-right order of their leaves. If none match, return [].

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 [5,4,8,11,null,13,4,7,2,null,null,5,1]
targetSum 22
returns [[5,4,11,2],[5,8,4,5]]

Two paths add to 22: 5,4,11,2 and 5,8,4,5.

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