#K78812. Symmetric Sum in a Binary Tree
Symmetric Sum in a Binary Tree
Symmetric Sum in a Binary Tree
You are given a binary tree represented in level order. Some nodes may be missing; these are denoted by -1
in the input. Your task is to compute the symmetric sum for each level of the tree.
For each level, if the level has n nodes with values a0, a1, ..., an-1, the symmetric sum is computed as follows:
\[ S = \sum_{i=0}^{\lfloor (n-1)/2 \rfloor} \Bigl(a_{i}+a_{n-1-i}\Bigr) \]
Note that if n is odd, the middle element is added only once. If the tree is empty (N = 0
), simply output nothing.
Input Format: The first line contains an integer N, the number of values in the level order representation. The following line contains N space-separated integers denoting node values (-1
representing missing nodes).
Output Format: Print the symmetric sum for each level in one line, with each level's result separated by a single space.
inputFormat
The input is given via standard input (stdin) and includes:
- An integer
N
representing the number of values in the level order traversal. - A sequence of
N
space-separated integers. The value-1
indicates that the corresponding child is missing.
outputFormat
Output the symmetric sum for each level of the binary tree in order, separated by a space. For an empty tree, output nothing.
## sample7
1 2 3 -1 -1 4 5
1 5 9