VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-depth-of-the-deepest-odd-level-node/

⇱ Find depth of the deepest odd level leaf node - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find depth of the deepest odd level leaf node

Last Updated : 23 Jul, 2025

Write a code to get the depth of the deepest odd level leaf node in a binary tree. Consider that level starts with 1. Depth of a leaf node is number of nodes on the path from root to leaf (including both leaf and root).
For example, consider the following tree. The deepest odd level node is the node with value 9 and depth of this node is 5. 

 1
 / \
 2 3
 / / \ 
 4 5 6
 \ \
 7 8
 / \
 9 10
 /
 11
Recommended Practice

The idea is to recursively traverse the given binary tree and while traversing, maintain a variable "level" which will store the current node's level in the tree. If current node is leaf then check "level" is odd or not. If level is odd then return it. If current node is not leaf, then recursively find maximum depth in left and right subtrees, and return maximum of the two depths.

Implementation:


Output
5 is the required depth

Time Complexity: The function does a simple traversal of the tree, so the complexity is O(n).

Auxiliary Space: O(h) where h is the height of the tree.

Iterative Approach: Traverse the tree in iterative fashion for each level, and whenever you encounter the leaf node, check if level is odd, if level is odd, then update the result. 

Implementation:


Output
5 is the required depth 

Time Complexity: Time Complexity is O(n).

Auxiliary Space: O(n)

Comment
Article Tags:
Article Tags: