Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Sum of Root To Leaf Binary Numbers

XPChallenge Points: 10
levelLevel: Easy

You’re given the root of a binary tree where each node’s value is 0 or 1. Every root-to-leaf path forms a binary number with the root as the most significant bit. For each leaf, interpret its path as a binary number and sum all such numbers. Return the total (it fits in 32-bit int).

Example 1:

Input: root = [1,0,1,0,1,0,1]

Output: 22

Explanation:

Paths (binary → decimal): 1→0→0 = 100₂ = 4 1→0→1 = 101₂ = 5 1→1→0 = 110₂ = 6 1→1→1 = 111₂ = 7 Sum = 4 + 5 + 6 + 7 = 22

Example 2:

Input: root = [0]

Output: 0

Example 3:

Input: Input: root = [1,1,1]

Output: 6

Explanation:

Paths: 1→1=11₂=3 (left), 1→1=3 (right), sum=6.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result