Question: Write a function int levelSearch(Node* root, int key) that takes as input the root node of a Binary Search tree and a key. The function

Write a function int levelSearch(Node* root, int key) that takes as input the root node of a Binary Search tree and a key. The function searches the key in the BST and returns the level of the found node. If the key is not found in the tree, return -1. The level starts at 0 for the root node and increases from top to bottom.

We have defined the following node C++ Node class for you:

class Node { public:  int name; Node* left = NULL;   Node* right = NULL; }; 

Note: In the test cases, the first line for input has multiple nodes and we will be inserting each one of them in your tree using an insert function. The second line is the key to be searched. The output is the level of the key in the BST.

Sample Input 1:

8 11 15 2 5 5

Sample Output 1:

2 

Sample Input 2:

8 11 15 2 5 8

Sample Output 2:

0

int levelSearch(Node* root, int key) { //your code here }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!