Ctrl K

Leaf Path With a Given Total

Easy
You get the root root of a binary tree and an integer total. Return True if some path from the root down to a leaf has node values adding up to exactly total, and False otherwise. A leaf is a node with no children. The path must start at the root and end at a leaf; it cannot stop halfway.
Example 1
root=tree_node([3, 9, -2, None, None, 4, 6]), total=7 → True

The path 3 → -2 → 6 adds up to 7, and 6 is a leaf.

Example 2
root=tree_node([2, 5]), total=2 → False

The root 2 is not a leaf because it has the child 5. The only root-to-leaf path 2 → 5 sums to 7.

Example 3
root=tree_node([]), total=0 → False

An empty tree has no paths at all.

Constraints
  • The tree has 0 to 5000 nodes
  • -1000 ≤ node value ≤ 1000
  • -1000 ≤ total ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsIdentical Trees EasyMirror-Image Tree EasyTree Height EasyNearest Leaf Depth EasyMirror the Tree Easy
loading Python…
Input
root=tree_node([3, 9, -2, None, None, 4, 6]), total=7
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 78 tests on the server, hidden ones included.