Hello friends!
And, welcome to another tutorial on GeeksforGeeks. In this video we are going to understand the
program which helps us in getting the level of a node in a binary tree. First us
see an example. In this example, if input key is 3, then function
should return 1 as node 3 is in level 1.. If input
key is 4, the functino should return 3 as node 4 is in level 3.. Now, let us see the
algorithm which will assist us in finding the level
of a given node. Let us also have a sample tree to test our
algorithm. We pass the root node for and the value of
node for which we have to find the level number to function getLevel. Let us
assume that we have to find level of node 1.. So, node will point to 3 and data
will be equal to 1. We call the getLevelUtil function with root
data and 1. So, in the getLevelUtil fuction, node will point to 3, data will be
equal to 1 and level will also be equal to 1. Since node is not null, we check if node’s
data is equal to data. As it is false, we have another variable downlevel which recursively
call the left and right child of root with level+1 using a call stack. So,
now node will point to 2 and level will be level+1=2 Again, since node’s data is not equal to
data, we call the left child of 2 with level +
1 ..Hence node will point to 1 and level will be equal to 3. Now, node->data is equal to data hence
we return level.. So, we return 3 which is the level number of node 1.. Now, let us see
the time complexity of the program. This code will run in O(n) complexity.Here
n are the number of nodes in our binary tree.
With this we come to an end of this tutorial. For any doubts or suggestions please leave
them in the comment section below. Thanks for watching!


NikhiL PandargE · September 27, 2017 at 3:31 pm

helpful for level….finding..thx

Rahul Sharma · September 27, 2017 at 6:23 pm

Now geeksforgeeks has become the most useful channel for learning and understanding Trees. The videos you are uploading are really helpful and what my professors are not able to explain in 3-4 lectures you guys are doing in minutes! Really awesome.

Prince Kumar · November 2, 2018 at 5:13 pm

at least u should also explain the right recursive function

batchen · December 2, 2018 at 4:43 pm

Very Helpful! thanks from Israel

tapanjeet roy · April 14, 2019 at 2:50 am

Bloody bastard..if you don't know how to explain then why you try

Sandro Montanaro · June 1, 2019 at 7:45 am

data and level. I listened to you at half speed, couldn't figure out what data and level is. since level is a function parameter, it can't be the level we are looking for.

pranjali nalavade · June 26, 2019 at 3:44 am

What is level of root node 0 or 1

Leave a Reply

Your email address will not be published. Required fields are marked *