You are given an arbitrary binary tree consisting of N nodes. Your task is to find the largest subtree sum.
Note :
1. The value of any node will not be equal to zero.
2. Subtree of a node X is all the nodes that are below X and X itself.
3. For example in the tree given below, nodes 1, 2 and 3 are in subtree of 1.

4. Subtree sum of X is the sum value of all the nodes in subtree of X.
5. Binary tree is a tree wherein each node has at most two children.
6. Any two nodes may have the same value associated with it.
First line of each input has a single integer T, denoting the number of test cases.
First line of each test case contains a single integer N, denoting the number of nodes in the tree.
The second line contains the values of the nodes of the tree in the level order form ( 0 for NULL node) Refer to the example for further clarification.
Example:
Consider the binary tree

The input of the tree depicted in the image above will be like:
1
2 3
4 0 5 6
0 7 0 0 0 0
0 0
Explanation :
7 is the number of nodes
Second-line contains the value of nodes from 1 to 7.
Then the structure of the tree follows.
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (0)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (0)
Right child of 4 = 7
Left child of 5 = null (0)
Right child of 5 = null (0)
Left child of 6 = null (0)
Right child of 6 = null (0)
Level 5 :
Left child of 7 = null (0)
Right child of 7 = null (0)
The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null (0).
Output format :
For each test case print a single integer on a new line, representing the maximum subtree sum.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10^2
1 <= N <= 3*10^3
10^(-9) <= VAL[i] <= 10*(9)
Where 'VAL' denotes the values of nodes in binary tree.
Time Limit : 1 sec.
1
6
-5 1 2 1 1 0 3 0 0 0 0 0 0 0
5
The above test case represents the following tree.

So if we calculate the subtree sum of all nodes we will get the following answer.
SubtreeSum[-5] = 3
SubtreeSum[1] = 3
SubtreeSum[2] = 5
SubtreeSum[1] = 1
SubtreeSum[1] = 1
SubtreeSum[3] = 3
So the largest subtree sum is 5.
2
7
10 -3 -3 1 1 1 1 0 0 0 0 0 0 0 0
1
-1 0 0
8
-1
In test case 1 , we are given the following tree.

So if we calculate the subtree sum of all nodes we will get the following answer.
SubtreeSum[10] = 8
SubtreeSum[-3] = -1
SubtreeSum[-3] = -1
SubtreeSum[1] = 1
SubtreeSum[1] = 1
SubtreeSum[1] = 1
SubtreeSum[1] = 1
So the largest subtree sum is 8.
In the second test case we have only one node so there will be only one subtree so we return its value as answer that is we return -1.
Think of using some extra space for storing nodes of the tree.
We will do a preorder traversal of the tree and store all the nodes of the tree in a preorder vector.
O(N^2), where N is the number of nodes in a tree.
Because for each node we are traversing its subtree in a preorder manner which is in O(N) and we have O(N) such nodes, so the overall complexity is O(N^2).
O(N) , where N is the number of nodes in a tree.
Because we are storing all the nodes of the tree in a preorder vector which will take O(N) memory space.