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.
Read full article from Find depth of the deepest odd level leaf node | GeeksforGeeks
int
depthOfOddLeafUtil(Node *root,
int
level)
{
// Base Case
if
(root == NULL)
return
0;
// If this node is a leaf and its level is odd, return its level
if
(root->left==NULL && root->right==NULL && level&1)
return
level;
// If not leaf, return the maximum value from left and right subtrees
return
max(depthOfOddLeafUtil(root->left, level+1),
depthOfOddLeafUtil(root->right, level+1));
}
/* Main function which calculates the depth of deepest odd level leaf.
This function mainly uses depthOfOddLeafUtil() */
int
depthOfOddLeaf(
struct
Node *root)
{
int
level = 1, depth = 0;
return
depthOfOddLeafUtil(root, level);
}