#K38467. Sum of Nodes with Even-Valued Parent
Sum of Nodes with Even-Valued Parent
Sum of Nodes with Even-Valued Parent
Given a binary tree, compute the sum of the values of all nodes that have an even-valued parent. The tree is provided as a level-order traversal where the string null
represents a missing node. Note that the root node does not have a parent and should never be counted, regardless of its value.
Your task is to construct the binary tree from the input and then determine the sum of all node values whose parent node's value is even. If the tree is empty, simply return 0.
The mathematical formulation of the result can be viewed as follows:
$$ \text{result} = \sum_{\substack{node \in Tree \\ parent(node) \text{ is even}}} node.val $$inputFormat
The input is given in two lines. The first line contains an integer N denoting the number of nodes in the binary tree. If N > 0, the second line contains N space-separated tokens representing the level-order traversal of the tree. Each token is either an integer (node value) or the string 'null' indicating a missing node. Note that if N = 0, the tree is empty.
outputFormat
Output a single integer which is the sum of the values of nodes that have an even-valued parent.## sample
1
1
0
</p>