Given a binary tree having a positive integer written on each of its nodes. Your task is to find the vertical sum of node values i.e. the sum of nodes that can be connected by a vertical line.
A binary tree is a tree in which each parent node has at most two children.
You are required to print the sum along vertical lines from the leftmost node and moving to the rightmost node.
For Example :Consider the following Binary Tree:

So the final answer is
12 9 11 6
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.
The first line of each test case contains the elements of the tree in the level order form separated by a single space. If any node does not have a left or right child, take -1 in its place.
For example, the input for the tree depicted in the below image would be :

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
Explanation :
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 (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)
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 (-1).
Note :
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output Format :
For each test case, print the vertical sum of node values separated by a space in a separate line.
1 <= T <= 10
1 <= N <= 3000
1 <= node data <= 10^9
Time Limit: 1sec
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
2
1 2 3 -1 10 5 6 -1 -1 -1 8 -1 -1 -1 -1
20 8 22 5 3 -1 -1 -1 -1 -1 -1
2 16 11 6
5 8 23 22
For the first test case:

As shown in the above figure,
Nodes connected by Line 1 = 2, Sum= 2
Nodes connected by Line 2= 1,5,10 Sum = 16 (1+5+10)
Nodes connected by Line 3= 3,8, Sum =11 (3+8)
Nodes connected by Line 4= 6, Sum= 6
So Our answer is
2 16 11 6
For the second test case :
Nodes connected by Line 1 = 5 Sum= 5
Nodes connected by Line 2 = 8 Sum = 8
Nodes connected by Line 3 =20,3 Sum =23 (20+3)
Nodes connected by Line 4 = 22 Sum= 22
So Our answer is
5 8 23 22
2
1 2 3 4 -1 6 7 -1 -1 -1 8 -1 -1 -1 -1
1 5 -1 6 -1 7 -1 -1 -1
4 2 7 11 7
7 6 5 1
Try to think of a data structure which can store the sum of nodes at the same horizontal distance from the root.
First, we see below example before discussing the approach:
For the above given binary tree:
1 is the root node, we say its horizontal distance = 0.
Since 2 lies to the left of 1, its horizontal distance from 1 is 0-1= -1
3 lies to the right of 1, its horizontal distance = 0+1 = 1
Similarly, horizontal distance of 4 = Horizontal distance of 2 - 1= -1-1=-2
Horizontal distance of 5 = Horizontal distance of 2 + 1= -1+1 = 0
Horizontal distance of 6 = Horizontal distance of 3 - 1 i.e 1-1 =0
Horizontal distance of 7 = Horizontal distance of 3 +1 i.e 1+1 = 2
So Nodes at horizontal distance -2 = 4 , Sum=4,
Nodes at horizontal distance -1 = 2 , Sum=2,
Nodes at horizontal distance 0 = 1,5,6 ,Sum=12 (1+5+6),
Nodes at horizontal distance 1= 3 , Sum=3,
And Nodes at horizontal distance 2= 7, Sum=7
So the final answer is 4 2 12 3 7
Algorithm : -
We will use an ordered map as we need to print the sequence in the sorted order of horizontal distance.
O(N*log(N)), where ‘N’ is the number of nodes present in the binary tree.
In the worst case, we iterate through all the nodes of the binary tree and the insertion and searching in the ordered map takes time of the order ‘log(N)’. Hence the overall time complexity will be O(N*log(N)).
O(N), where ‘N’ is the number of nodes present in the binary tree.
In the worst case, the map will have 'N' key-value pairs, Hence the overall space complexity will be O(N).