Depth of the deepest odd level node in Binary Tree in C++ Program

In this tutorial, we are going to learn how to find the deepest odd level node in a binary tree.

This is similar to finding the depth of the binary tree. Here, we have to one more condition i.e.., whether the current level is odd or not.

Let's see the steps to solve the problem.

  • Initialize the binary tree with dummy data.

  • Write a recursive function to find the deepest odd level node in a binary tree.

    • If the current node is a leaf node and the level is odd, then return the current level.

    • Else return the max of left node and right node with recursive function calls.

  • Print the deepest odd level node.


Let's see the code.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
struct Node {
   int data;
   struct Node *left, *right;
struct Node* newNode(int data) {
   struct Node* node = (struct Node*) malloc(sizeof(struct Node));
   node->data = data;
   node->left = node->right = NULL;
   return node;
int oddLeafDepthInTree(struct Node *root, int level) {
   if (root == NULL) {
      return 0;
   if (root->left == NULL && root->right == NULL && level % 2 == 1) {
      return level;
   return max(oddLeafDepthInTree(root->left, level + 1), oddLeafDepthInTree(root->right, level + 1));
int main() {
   struct Node* root = newNode(1);
   root->left = newNode(2);
   root->right = newNode(3);
   root->left->left = newNode(4);
   root->right->left = newNode(5);
   root->right->right = newNode(6);
   root->right->left->right = newNode(7);
   root->right->right->right = newNode(8);
   int level = 1, depth = 0;
   cout << oddLeafDepthInTree(root, level) << endl;
   return 0;


If you execute the above code, then you will get the following result.



If you have any queries in the tutorial, mention them in the comment section.

Updated on: 27-Jan-2021


Kickstart Your Career

Get certified by completing the course

Get Started