Depth of the deepest odd level node in Binary Tree
Given a Binary tree, find out depth of the deepest odd level leaf node. Take root level as depth 1.
Examples:
Input :

Output : 5
Input : 10
/ \
28 13
/ \
14 15
/ \
23 24
Output : 3We can traverse the tree starting from the root level and keep curr_level of the node.
Increment the curr_level each time we go to left or a right subtree.
Return the max depth of an odd level,if it exists.
Algorithm:
1) return 0 if curr_node == NULL
2) if curr_node is leaf and curr_level is odd,
return curr_level
3) else maximum(depthOdd(left subtree),
depthOdd(right subtree))Below is the implementation.
C++
// C++ program to find depth of the deepest// odd level node#include<bits/stdc++.h>using namespace std;// A Tree nodestruct Node{ int key; struct Node *left, *right;};// Utility function to create a new nodeNode* newNode(int key){ Node* temp = new Node; temp->key = key; temp->left = temp->right = NULL; return (temp);}// Utility function which// returns whether the current node// is a leaf or notbool isleaf(Node *curr_node){ return (curr_node->left == NULL && curr_node->right == NULL);}// function to return the longest// odd level depth if it exists// otherwise 0int deepestOddLevelDepthUtil(Node *curr_node, int curr_level){ // Base case // return from here if ( curr_node == NULL) return 0; // increment current level curr_level += 1; // if curr_level is odd // and its a leaf node if ( curr_level % 2 != 0 && isleaf(curr_node)) return curr_level; return max(deepestOddLevelDepthUtil(curr_node->left,curr_level), deepestOddLevelDepthUtil(curr_node->right,curr_level));}// A wrapper over deepestOddLevelDepth()int deepestOddLevelDepth(Node *curr_node){ return deepestOddLevelDepthUtil(curr_node, 0);}// Driver codeint main(){ /* 10 / \ 28 13 / \ 14 15 / \ 23 24 Let us create Binary Tree shown in above example */ Node *root = newNode(10); root->left = newNode(28); root->right = newNode(13); root->right->left = newNode(14); root->right->right = newNode(15); root->right->right->left = newNode(23); root->right->right->right = newNode(24); cout << deepestOddLevelDepth(root) << endl; return 0;} |
Java
// Java program to find depth of the deepest// odd level nodeclass GfG {// A Tree nodestatic class Node{ int key; Node left, right;}// Utility function to create a new nodestatic Node newNode(int key){ Node temp = new Node(); temp.key = key; temp.left = null; temp.right = null; return (temp);}// Utility function which// returns whether the current node// is a leaf or notstatic boolean isleaf(Node curr_node){ return (curr_node.left == null && curr_node.right == null);}// function to return the longest// odd level depth if it exists// otherwise 0static int deepestOddLevelDepthUtil(Node curr_node, int curr_level){ // Base case // return from here if ( curr_node == null) return 0; // increment current level curr_level += 1; // if curr_level is odd // and its a leaf node if ( curr_level % 2 != 0 && isleaf(curr_node)) return curr_level; return Math.max(deepestOddLevelDepthUtil(curr_node.left,curr_level), deepestOddLevelDepthUtil(curr_node.right,curr_level));}// A wrapper over deepestOddLevelDepth()static int deepestOddLevelDepth(Node curr_node){ return deepestOddLevelDepthUtil(curr_node, 0);}public static void main(String[] args){ /* 10 / \ 28 13 / \ 14 15 / \ 23 24 Let us create Binary Tree shown in above example */ Node root = newNode(10); root.left = newNode(28); root.right = newNode(13); root.right.left = newNode(14); root.right.right = newNode(15); root.right.right.left = newNode(23); root.right.right.right = newNode(24); System.out.println(deepestOddLevelDepth(root));}} |
Python3
# Python3 program to find depth of# the deepest odd level node# Helper function that allocates a# new node with the given data and# None left and right poers. class newNode: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None# Utility function which returns# whether the current node is a# leaf or notdef isleaf(curr_node) : return (curr_node.left == None and curr_node.right == None)# function to return the longest# odd level depth if it exists# otherwise 0def deepestOddLevelDepthUtil(curr_node, curr_level) : # Base case # return from here if (curr_node == None) : return 0 # increment current level curr_level += 1 # if curr_level is odd and # its a leaf node if (curr_level % 2 != 0 and isleaf(curr_node)) : return curr_level return max(deepestOddLevelDepthUtil(curr_node.left, curr_level), deepestOddLevelDepthUtil(curr_node.right, curr_level))# A wrapper over deepestOddLevelDepth()def deepestOddLevelDepth(curr_node) : return deepestOddLevelDepthUtil(curr_node, 0) # Driver Codeif __name__ == '__main__': """ 10 / \ 28 13 / \ 14 15 / \ 23 24 Let us create Binary Tree shown in above example """ root = newNode(10) root.left = newNode(28) root.right = newNode(13) root.right.left = newNode(14) root.right.right = newNode(15) root.right.right.left = newNode(23) root.right.right.right = newNode(24) print(deepestOddLevelDepth(root))# This code is contributed by# Shubham Singh(SHUBHAMSINGH10) |
C#
// C# program to find depth of the deepest// odd level nodeusing System;class GfG{ // A Tree node class Node { public int key; public Node left, right; } // Utility function to create a new node static Node newNode(int key) { Node temp = new Node(); temp.key = key; temp.left = null; temp.right = null; return (temp); } // Utility function which // returns whether the current node // is a leaf or not static bool isleaf(Node curr_node) { return (curr_node.left == null && curr_node.right == null); } // function to return the longest // odd level depth if it exists // otherwise 0 static int deepestOddLevelDepthUtil(Node curr_node, int curr_level) { // Base case // return from here if ( curr_node == null) return 0; // increment current level curr_level += 1; // if curr_level is odd // and its a leaf node if ( curr_level % 2 != 0 && isleaf(curr_node)) return curr_level; return Math.Max(deepestOddLevelDepthUtil(curr_node.left,curr_level), deepestOddLevelDepthUtil(curr_node.right,curr_level)); } // A wrapper over deepestOddLevelDepth() static int deepestOddLevelDepth(Node curr_node) { return deepestOddLevelDepthUtil(curr_node, 0); } public static void Main(String[] args) { /* 10 / \ 28 13 / \ 14 15 / \ 23 24 Let us create Binary Tree shown in above example */ Node root = newNode(10); root.left = newNode(28); root.right = newNode(13); root.right.left = newNode(14); root.right.right = newNode(15); root.right.right.left = newNode(23); root.right.right.right = newNode(24); Console.WriteLine(deepestOddLevelDepth(root)); }}// This code is contributed by PrinciRaj1992 |
Javascript
<script>// JavaScript program to find depth of the deepest// odd level node// A Tree nodeclass Node{ constructor() { this.key = 0; this.left = null; this.right = null; }}// Utility function to create a new nodefunction newNode(key){ var temp = new Node(); temp.key = key; temp.left = null; temp.right = null; return (temp);}// Utility function which// returns whether the current node// is a leaf or notfunction isleaf(curr_node){ return (curr_node.left == null && curr_node.right == null);}// function to return the longest// odd level depth if it exists// otherwise 0function deepestOddLevelDepthUtil(curr_node, curr_level){ // Base case // return from here if ( curr_node == null) return 0; // increment current level curr_level += 1; // if curr_level is odd // and its a leaf node if ( curr_level % 2 != 0 && isleaf(curr_node)) return curr_level; return Math.max(deepestOddLevelDepthUtil(curr_node.left,curr_level), deepestOddLevelDepthUtil(curr_node.right,curr_level));}// A wrapper over deepestOddLevelDepth()function deepestOddLevelDepth(curr_node){ return deepestOddLevelDepthUtil(curr_node, 0);}/* 10/ \28 13 / \ 14 15 / \ 23 24Let us create Binary Tree shown in above example */var root = newNode(10);root.left = newNode(28);root.right = newNode(13);root.right.left = newNode(14);root.right.right = newNode(15);root.right.right.left = newNode(23);root.right.right.right = newNode(24);document.write(deepestOddLevelDepth(root));</script> |
Output:
3
This article is contributed by Shubham Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




