Associated Course(s):
DSA-Self Paced with Doubt Assistance
Placement 100

Given a Binary Tree of **N** nodes and a positive integer **k**. The task is to count all distinct nodes that are distance k from a leaf node. Here, k distance from leaf means k levels higher than a leaf node. For example, if k is more than height of Binary Tree, then nothing should be counted.

**Input Format:**

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer N denoting the number of edges. The next line contains edges of the binary tree. The last line contains the value of k.

**Output Format:**

Count the distinct number of nodes that are at distance k from the leaf nodes.

**Your Task:**

You don't have to take input. Complete the function **printKDistantfromLeaf **that takes **node and k **as parameters and **returns **the number of nodes that are at distance k from the leaf nodes.

**Constraints:**

1 <= T <= 100

1 <= N <= 10^{2}

**Example:
Input:**

2

7

1 2 L 2 4 L 2 5 R 1 3 R 3 6 L 6 8 R 3 7 R

2

5

1 3 L 3 5 L 5 7 L 5 8 R 8 9 R

4

**Output:**

2

1

Author: Ayush Govil 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

RatneshTiwari3 | 24 |

AnmolPassi | 20 |

Swords | 20 |

baranwalsuraj96 | 17 |

tarun711 | 16 |

Bishnu Dev Panda | 727 |

yk12 | 512 |

DevMittal | 472 |

okayboss | 458 |

VijethaShastri | 431 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4921 |

Quandray | 4547 |

Login to report an issue on this page.