You are given a Binary Tree and an integer ‘DISTANCE’, Your task is to calculate the number of good leaf node pairs in the tree.
Note :
A pair of two different leaf nodes of a binary tree is said to be good if the length of the shortest path between them is less than or equal to ‘DISTANCE’.
The first line contains an Integer 'T' which denotes the number of test cases to be run. Then the test cases follow.
The first line of each test case contains one integer ‘DISTANCE’, as described in the problem statement.
The second 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.
Output Format :
For each test case, print the number of good leaf node pairs in the tree.
The output of every test case will be printed in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= Number of Nodes in the tree,N <= 2^10
1 <= DISTANCE <= 10
1 <= Node value <= 10000
Time Limit: 1sec
1
2
1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1
2
Only Pairs {4,5} and {6,7} are good leaf node pairs since the shortest distance between them is 2 and the shortest distance between all the other pairs are greater than 2.

1
3
1 2 3 4 -1 -1 5 -1 -1 -1 -1
0
Since, there are only 2 leaf nodes 4 and 5 and the shortest distance between them is 4 so the answer is 0.

At each node, calculate the number of good leaf nodes separately.
O(N^2), where 'N is the number of nodes in the tree.
If we see the computation cost at each node, we will get the worst case when the tree is a complete binary tree and for this case, the number of operations will be like this:
((N-1)/2)^2 + 2*((N-3)/4)^2 + 4*((N-5)/8)^2 + …. And it will give the upper bound to be O(N^2).
O(N), where N is the number of nodes in the tree.
At each node, we are returning a list containing all leaf nodes of its subtree. So at-max we can have N/2 leaf nodes for a complete binary tree. Also, we will have a call stack of size H where H is the height of the tree. So space complexity is O(H+N) where H can be at max N. So overall space complexity is O(N).