#C3492. Leaf Path Sum in a Binary Tree

    ID: 46925 Type: Default 1000ms 256MiB

Leaf Path Sum in a Binary Tree

Leaf Path Sum in a Binary Tree

You are given a binary tree where each node contains a non-negative integer. Each root-to-leaf path represents a number formed by concatenating the values of the nodes along the path. Your task is to compute the sum of all these numbers. For instance, consider the binary tree below:

    1
   / \
  2   3

The tree has two root-to-leaf paths: 1 → 2 which represents the number \(12\), and 1 → 3 which represents \(13\). The sum is \(12 + 13 = 25\).

Input/Output Specification:

You will be given the binary tree in a single line in level-order (breadth-first) traversal format. The nodes are separated by spaces, and a missing (null) node is represented by the string null. Your program should build the tree, compute the sum of all root-to-leaf numbers, and output the result.

All formulas must be represented in LaTeX format. For instance, the sum can be written as \(S = \sum{\text{path number}}\).

inputFormat

The input is a single line containing space-separated tokens representing the binary tree in level-order. For example:

1 2 3

Here, "1" is the root, "2" is the left child, and "3" is the right child. Use the token null to represent an absent child.

outputFormat

Output a single integer representing the sum of all numbers formed by root-to-leaf paths.

For example, for the input above, the output would be:

25
## sample
1 2 3
25