2Brute2Best
🔥0

Sum Root to Leaf Numbers

Medium·LeetCode #129 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given the root of a binary tree. Every node holds one digit from 0 to 9.

Read a root-to-leaf path as a number, from top to bottom. For example, the path 1 → 2 → 3 is the number 123. Return the total of the numbers of all root-to-leaf paths. The tests are built so that the total fits in a 32-bit integer.

Input

The root of a binary tree. It has at least one node, and each value is a digit from 0 to 9.

Output

A single number: the total of all root-to-leaf numbers.

Example 1
Input: root = [1, 2, 3]
Output: 25

The paths read 12 (1 → 2) and 13 (1 → 3). 12 + 13 = 25.

Example 2
Input: root = [4, 9, 0, 5, 1]
Output: 1026

The paths read 495 (4 → 9 → 5), 491 (4 → 9 → 1) and 40 (4 → 0). 495 + 491 + 40 = 1026.

Constraints
  • • The tree has between 1 and 1000 nodes.
  • • 0 ≤ Node.val ≤ 9
  • • The depth of the tree is at most 10.
  • • The answer fits in a 32-bit integer.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“each root-to-leaf path represents a number”
→ The path is read as digits. Its number changes by one digit at each step down.
“digits from 0 to 9”
→ Each step is one digit, so the update is multiply by 10 and add.
“return the total”
→ The number from every leaf is added up, so the answer is a sum over the leaves.
03

Approach Complexity Ladder

Brute Force — build a digit string per path, convert at the endO(N · H)O(L · H)

Write each path down as digits, then convert each string to a number and add

Optimal — carry cur = cur × 10 + digitO(N)O(H)

Multiply the number so far by 10 and add the digit; add the number at each leaf

Brute Force Intuition

Walk the tree and add each node's digit to a string that is passed down. At each leaf, save the whole string. At the end, convert every saved string to an int with stoi and add them up.

Recognition rule for this sub-pattern:

Carry information down the path (current sum, max so far, direction). Check it at leaves.
Remember this · Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.