Level of a Node in Binary Tree
Submissions: 5292   Accuracy:

56.86%

  Difficulty: Easy   Marks: 2

Given a Binary Tree and a target key, your task is to complete the function getLevel() that returns the level of the key from the given binary tree.

           3
         /   \
        2     5
      /   \
     1     4
Key: 4
Level: 3  

Note: if no such key exsits then return 0.
Input:
The function takes 2 arguments as input, first the reference pointer to the root node of the binary tree and a integer value target. There will be multiple test cases and for each test the function will be called seperately.

Output:
For each test return the level of the target value from the binary tree.

Constraints:
1<=T<=100
1<=N<=100

Example:
Input:

2
2
1 2 L 1 3 R
2
5
1 2 R 1 3 L 2 4 L 2 5 R 4 7 L
7
Ouput:
2
4

Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

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

Author: harshitsidhwa


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.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.