Last Updated: 23 Mar, 2021

Number Of Good Leaf Nodes Pairs

Easy
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’.
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

Approaches

01 Approach

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

02 Approach

  • In this approach, we will again keep two vectors leftDistance and rightDistance.
  • leftDistance[i] will store the number of leaf nodes with distance i from the current node in its left subtree.
  • rightDistance[i] will store the number of leaf nodes with distance ‘i’ from the current node in its right subtree.
  • Then at each node, we will find the leftDistance and rightDistance vector and then we will iterate on each pair of distance (say i and j respectively) and if (i+j) <= DISTANCE, then leftDistance[i]*rightDistance[j] will be added to the answer.
    • For calculating the leftDistance vector, if the current node is a leaf node then it will be a vector with size DISTANCE and all elements will be 0 or else it will be calculated by using the leftDistance and rightDistance of its subtree as leftDistance[i] = leftDistanceOfChild[i-1] + rightDistanceOfChild[i-1].
    • Similarly, we will calculate the rightDistance vector.