#K96072. Sum of Root-to-Leaf Numbers
Sum of Root-to-Leaf Numbers
Sum of Root-to-Leaf Numbers
You are given a binary tree represented in level order by a sequence of values separated by spaces. In this representation, the keyword null
denotes an absent child node. For each root-to-leaf path, a number is formed by concatenating the node values along that path. Formally, if a path from the root to a leaf has node values (a_1, a_2, \ldots, a_n), then the number corresponding to this path is given by
[
\text{number} = a_1 \times 10^{n-1} + a_2 \times 10^{n-2} + \cdots + a_n \times 10^0
]
Your task is to compute the sum of all these numbers for every root-to-leaf path in the tree.
Note: A leaf is a node with no children.
inputFormat
The input consists of a single line containing space-separated tokens that represent the binary tree in level order. Each token is either an integer or the string null
(without quotes) which represents a missing node.
outputFormat
Output a single integer which is the sum of all the numbers formed from root-to-leaf paths.## sample
1 2 3
25