ExamAdda Logo

Sum Root to Leaf Numbers

Easy

You are given the root of a binary tree containing digits from 0 to 9.

Each root-to-leaf path represents a number formed by concatenating the digits along the path.

Return the total sum of all root-to-leaf numbers.

A leaf is a node with no children.

Example 1

Input

root = [1,2,3]

Output

25

Explanation

There are two root-to-leaf paths:

1 → 2 = 12

1 → 3 = 13

 

Therefore:

12 + 13 = 25

Example 2

Input

root = [4,9,0,5,1]

Output

1026

Explanation

The root-to-leaf numbers are:

4 → 9 → 5 = 495

4 → 9 → 1 = 491

4 → 0 = 40

 

Therefore:

495 + 491 + 40 = 1026

Constraints

1 <= Number of nodes <= 1000
0 <= Node.val <= 9

Hints:

Hint 1

Build the number represented by the current root-to-node path while traversing the tree.

Hint 2

When you reach a leaf, add the number formed by that root-to-leaf path to the total sum.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 19, 2026

Last updated

Aug 20, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output