Skip to interview prep
Apexed

19. Root-to-Leaf Path Sum

EasyTrees

Return true if some root-to-leaf path adds up to target.

A binary tree is passed as TreeNode objects with val, left and right. The tests write trees in level order, with null for a missing child.

Example 1

Input: root = [5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, 1], target = 22
Output: true

Constraints

  • 0 ≤ nodes ≤ 5000

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Root-to-Leaf Path Sum — coding interview practice — Apexed