Asked at
Path Sum
EasyVerifiedTreeDFSRecursion~15 min
Given the root of a binary tree and a targetSum, return whether any root-to-leaf path has values that add up to targetSum.
A leaf is a node with no children. A node is { val, left, right } with null children. The input arrives as a single object { root, targetSum }.
Examples
in{ root: {val:1, left:{val:2}, right:{val:3}}, targetSum: 3 }
outtrue
Root-to-leaf path 1 → 2 sums to 3.
in{ root: null, targetSum: 0 }
outfalse
An empty tree has no root-to-leaf path.
Constraints
- 0 ≤ number of nodes ≤ 5000
- -1000 ≤ node.val ≤ 1000
- -1000 ≤ targetSum ≤ 1000
Get help
🔑
Sign in to solve
Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.