Given a binary tree whose node values are 0 or 1, treat each root-to-leaf path as a binary number and return the sum of all such numbers.
You are given the root of a binary tree where each node contains either 0 or 1.
Every root-to-leaf path forms a binary number by reading the node values from top to bottom. For example, a path 1 -> 0 -> 1 represents the binary number 101, which is 5 in decimal.
Return the sum of the decimal values of all root-to-leaf binary numbers in the tree.
A leaf is a node with no children.
root.0 or 1.0 or 1.Example 1
Input
root = [1,0,1,0,1,0,1]
Output
22
Explanation
The root-to-leaf paths are 100 = 4, 101 = 5, 110 = 6, and 111 = 7. Their sum is 22.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.