Description
Given a binary tree where each node has value 0 or 1, each root-to-leaf path represents a binary number. Return the sum of all root-to-leaf binary numbers.
Examples
Input:
root = [1,0,1,0,1,0,1]Output:
22Explanation:
Paths: 100,101,110,111 = 4+5+6+7=22.
Input:
root = [0]Output:
0Explanation:
A single-node tree with value 0 has one root-to-leaf path representing binary 0 = decimal 0.
Input:
root = [1,1,0,1,0]Output:
13Explanation:
The tree [1,1,0,1,0] has root-to-leaf paths: 1->1->1 (binary 111 = 7) and 1->1->0 (binary 110 = 6). Sum = 7 + 6 = 13.
Constraints
- •
1 ≤ nodes ≤ 1000