#K83057. Smallest Depth Zero Sum
Smallest Depth Zero Sum
Smallest Depth Zero Sum
You are given a binary tree. Your task is to find the sum of all node values at the smallest (shallowest) depth that contains at least one node with a value of \(0\). In other words, perform a level order traversal and as soon as you encounter a level where there is at least one \(0\) node, compute the sum of all nodes in that level that are equal to \(0\) (note that adding several zeros still gives \(0\)). If no node with the value \(0\) is found in the entire tree, output \(-1\).
Note: The input is given in a single line as the level order traversal of the binary tree. The value null
represents an absent (missing) node. All node values are integers.
For example, consider the following tree (Test Case 1):
1 2 3 4 0 0 5 null null 6 7
This represents the binary tree:
1
/ \
2 3
/ \ / \
4 0 0 5
/ \
6 7
The third level (depth = 3) has nodes with values 4, 0, 0, 5
. Since there are two nodes with value (0) on that level, the sum is (0 + 0 = 0), so the answer is 0
. If no level contains any (0), the output should be -1
.
inputFormat
The input consists of a single line containing the level order traversal of the binary tree. The values are separated by spaces, and the string null
is used for missing nodes.
Examples:
1 2 3 4 0 0 5 null null 6 7
1 2 3 null 0 0 null
1 2 3 4 5
outputFormat
Print a single integer representing the sum of all \(0\) values at the first level (from the root) that contains at least one \(0\) node. If no such level exists, print \(-1\).
## sample1 2 3 4 0 0 5 null null 6 7
0