#C5126. Sum of Root-to-Leaf Binary Numbers
Sum of Root-to-Leaf Binary Numbers
Sum of Root-to-Leaf Binary Numbers
You are given a binary tree where each node contains a value of either 0 or 1. Each root-to-leaf path in the tree represents a binary number obtained by concatenating the values along the path. Your task is to compute the sum of these binary numbers.
The binary number of a path with nodes having values \(b_1, b_2, \ldots, b_k\) is given by \[ N = b_1 \times 2^{k-1} + b_2 \times 2^{k-2} + \cdots + b_k \times 2^{0}, \] and you should output the total sum for all root-to-leaf paths.
Note: The input binary tree is provided in level order as a list enclosed in square brackets, with the string null
representing a missing node. For example: [1,0,1,0,1,0,1]
.
inputFormat
The input is provided via stdin as a single line string representing the level order traversal list of the binary tree. The list is enclosed in square brackets and node values are separated by commas. Use the token null
(case-sensitive) for missing nodes.
Example: [1,0,1,0,1,0,1]
outputFormat
Output a single integer to stdout which is the sum of all binary numbers represented by the root-to-leaf paths.
## sample[1,0,1,0,1,0,1]
22