BETA

Submissions: 1469 Accuracy: 25.19%

Node at distance (Function Problem)

Given a Binary Tree and a positive integer k, count all distinct nodes that are distance k from a leaf node. Here k distance from a 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. Expected time complexity is O(n) where n is the number nodes in the given Binary Tree.

Input:
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:
Count the distinct number of nodes that are at distance k from the lead nodes.

Constraints:
1<=T<=100
1<=n<=100
1<=data of node<=100

Example:
Input:

2
7
1 2 L 2 4 R 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

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Ayush Govil

It is recommended to 'Compile & Test' your code before clicking 'Submit'!

Compilation/Execution Result: