Given a Binary Tree and a key, write a function that returns level of the key.
For example, consider the following tree. If the input key is 3, then your function should return 1. If the input key is 4, then your function should return 3. And for key which is not present in key, then your function should return 0.
Recursive approach to this problem is discussed here
The iterative approach is discussed below :
The iterative approach is modified version of Level Order Tree Traversal
create a empty queue q push root and then NULL to q loop till q is not empty get the front node into temp node if it is NULL, it means all nodes of one level are traversed, so increment level else check if temp data is equal to data to be searched if yes then return level if temp->left is not NULL, enqueue temp->left if temp->right is not NULL, enqueue temp->right if value not found, then return 0
level of node 30 is 2 level of node 12 is 4 level of node 25 is 3 node 27 not found
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Deepest left leaf node in a binary tree | iterative approach
- Deepest right leaf node in a binary tree | Iterative approach
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Check for Symmetric Binary Tree (Iterative Approach)
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Iterative approach to check if a Binary Tree is Perfect
- Iterative approach to check for children sum property in a Binary Tree
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Get Level of a node in a Binary Tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Print all nodes at distance K from given node: Iterative Approach
- Difference between sums of odd level and even level nodes of a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Iterative Approach to check if two Binary Trees are Isomorphic or not
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Smallest number in BST which is greater than or equal to N ( Iterative Approach)