DSA · Trees · #173
Sum Root to Leaf Numbers
Module 44 · difficulty 3/5·⏱ 30:00starts on first keystroke
You are given the `root` of a binary tree where each node contains a single digit from `0` to `9`. Each root-to-leaf path represents a number formed by concatenating the digits along the path (the root digit is the most significant). Return the **total sum** of all root-to-leaf numbers. A **leaf** is a node with no children. The answer is guaranteed to fit in a 32-bit integer. Implement `sumNumbers(root)` where `root` is the root node (`{ val, left, right }`) or `null`.
Examples
root = [1,2,3]→25— Path 1->2 = 12, path 1->3 = 13. 12 + 13 = 25.root = [4,9,0,5,1]→1026— Paths: 4->9->5 = 495, 4->9->1 = 491, 4->0 = 40. 495 + 491 + 40 = 1026.root = [5]→5— A single node is itself a leaf forming the number 5.
Constraints
- · The number of nodes is in the range [1, 1000].
- · 0 <= Node.val <= 9
- · The depth of the tree will not exceed 10.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.