#K71427. Sum of Leaf Nodes in a Binary Tree

    ID: 33529 Type: Default 1000ms 256MiB

Sum of Leaf Nodes in a Binary Tree

Sum of Leaf Nodes in a Binary Tree

You are given a binary tree represented in level order format where missing nodes are denoted by the string NULL. Your task is to compute the sum of all leaf nodes in the tree.

A leaf node is defined as a node with no left or right child. Mathematically, if the leaf node values are \(v_1,v_2,\ldots,v_k\), then the required sum is given by:

[ S = \sum_{i=1}^{k} v_i ]

The tree is built using a level order traversal. For each test case, you will be given the number of nodes and the list of node values. Use this information to construct the binary tree and compute the sum of its leaf nodes.

inputFormat

The first line of input contains an integer T, the number of test cases.
For each test case:

  • The first line contains an integer N representing the number of nodes provided in level order.
  • The second line contains N space-separated values. Each value is either an integer (representing a node's value) or the string NULL which denotes a missing node.

Input is read from standard input (stdin).

outputFormat

For each test case, output a single integer — the sum of the leaf nodes in the binary tree. Each result should be printed on a new line to standard output (stdout).

## sample
2
7
1 2 3 4 5 6 7
3
10 20 30
22

50

</p>