Update appNew update is available. Click here to update.

Number Of Good Leaf Nodes Pairs

Last Updated: 23 Mar, 2021
Difficulty: Easy


Try Problem

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’.
Input Format :
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