Root-to-Leaf Paths with a Target Sum
Updated
Problem statement
Given the root of a binary tree and an integer targetSum, return every root-to-leaf path whose node values add up to targetSum.
A leaf has no children. Return paths in left-to-right depth-first order, with each path listed from the root to its leaf.
Function
pathSum(root: TreeNode, targetSum: int) → List<List<Integer>>Examples
Example 1
root = [5,4,8,11,null,13,4,7,2,null,null,5,1]targetSum = 22return = [[5,4,11,2],[5,8,4,5]]Both listed root-to-leaf paths sum to 22, and the left path is visited first.
Example 2
root = [1,2,3]targetSum = 5return = []Neither root-to-leaf path sums to 5.
Example 3
root = []targetSum = 0return = []An empty tree has no root-to-leaf path.
Constraints
- The tree contains between
0and500nodes. -1000 ≤ Node.val ≤ 1000.-10^9 ≤ targetSum ≤ 10^9.
More Bloomberg LP Onsite Interview questions
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Root-to-Leaf Paths with a Target SumAmazon · ONSITE INTERVIEW
- All Root-to-Leaf Paths in a Binary TreeGoogle · ONSITE INTERVIEWGoogle interview questions
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Find Leaves of a Binary TreeLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
- JSON Path Lookup with Implicit Array WildcardsMongoDB · ONSITE INTERVIEWMongoDB interview questions
- Delete and Compact a Forest SubtreePinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Onion-Peel Layers of a Binary TreeeBay · ONSITE INTERVIEWeBay interview questions