
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
Example
Let's see the code.
#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; }
Output
If you execute the above code, then you will get the following result.
3
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Depth of the deepest odd level node in Binary Tree in C++?
- Find the Deepest Node in a Binary Tree in C++
- Deepest left leaf node in a binary tree in C++
- Program to find second deepest node in a binary tree in python
- C++ program to Replace a Node with Depth in a Binary Tree
- Golang program to find the depth of a node in a binary search tree
- Program to find leftmost deepest node of a tree in Python
- C++ Program to Find Deepest Left Leaf in a Binary Tree
- Minimum Depth of Binary Tree in C++
- Find Minimum Depth of a Binary Tree in C++
- Program to perform level order traversal of binary tree in C++
- Difference between sums of odd level and even level nodes of a Binary Tree in Java
- Maximum Depth of Binary Tree in Python
- Binary Tree Level Order Traversal in C++
- Preorder Successor of a Node in Binary Tree in C++
