#K61222. BST Sum Computation

    ID: 31262 Type: Default 1000ms 256MiB

BST Sum Computation

BST Sum Computation

You are given several test cases. In each test case, an integer n is provided, followed by n unique integers. Insert these integers into a binary search tree (BST) following the BST insertion rules. The BST is defined such that for any node, its left subtree contains nodes with values < the node's value, and its right subtree contains nodes with values ≥ the node's value.

Once the BST is constructed for a test case, compute the sum of all values stored in the tree. Use recursion for both the insertion process and the summation. The answer for each test case should be output on a new line.

In mathematical notation, if the tree nodes have values \(v_i\), then the sum is \(\sum_{i} v_i\).

inputFormat

The first line contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N (the number of nodes). The second line contains N space-separated integers representing the node values to be inserted into the BST in order.

outputFormat

For each test case, output a single line containing the sum of all values in the BST.## sample

1
1
100
100

</p>