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
Template:
int levelSearch(Node* root, int key) { //your code here }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started