Number Of Good Leaf Nodes Pairs

Easy
0/40
Average time to solve is 15m
profile
Contributed by
7 upvotes
Asked in companies
Microsoftalibaba

Problem statement

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’.
Detailed explanation ( Input/output format, Notes, Images )
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.
Constraints:
1 <= T <= 10
1 <= Number of Nodes in the tree,N <= 2^10
1 <= DISTANCE <= 10
1 <= Node value <= 10000

Time Limit: 1sec
Sample Input 1 :
1
2
1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1
Sample Output 1 :
2
Explanation For Sample Input 1 :
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.

altImage

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

altImage

Hint

At each node, calculate the number of good leaf nodes separately.

Approaches (2)
Brute Force Approach Over Storing Leaves At Each Node
  • In this approach, we will keep two vectors leftLeaves and rightLeaves.
  • leftLeaves will keep the distance between the current node and all the leaf nodes towards the left of it.
  • rightLeaves will keep the distance between the current node and all the leaf nodes towards the right of it.
  • Then at each node, we will find the leftLeaves and rightLeaves vector and then we will iterate on each pair of elements in these vectors (say x and y) and check if (x + y) <= DISTANCE and if it is true then we will count this pair in the answer.
    • For calculating the leftLeaves vector, if the current node is a leaf node then it will be an empty vector or else it will be calculated by merging the leftLeaves and rightLeaves vector of its left child. Then we will increment each element of this merged vector by 1.
    • Similarly, we will calculate the rightLeaves vector.
Time Complexity

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).

Space Complexity

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).

Code Solution
(100% EXP penalty)
Number Of Good Leaf Nodes Pairs
Full screen
Console