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).
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
Input: root = [0]
Output: 0
Input: Input: root = [1,1,1]
Output: 6
Explanation:
Paths: 1→1=11₂=3 (left), 1→1=3 (right), sum=6.
Accepted:
Submission: