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

C++Server Side ProgrammingProgramming

Let us first define the struct that would represent a tree node that contains the int key and its left and right node child. If this is the first node to be created then it’s a root node otherwise a child node.

struct Node {
   int data;
   struct Node *leftChild, *rightChild;
};

Next we create our createNode(int key) function that takes an int key value and assign it to the key member of the node. The function returns the pointer to the created struct Node. Also, the left and right child of the newly created node are set to null.

Node* createNode(int data){
   Node* node = new Node;
   node->data = data;
   node->leftChild = node->rightChild = NULL;
   return node;
}

Next we have our isLeaf(Node *currentNode) function that takes a node and checks if it has any children or not. It returns true or false based if the node is leaf node or not.

bool isLeaf(Node *currentNode){
   return (currentNode->leftChild == NULL &&
   currentNode->rightChild == NULL);
}

The deepestOddLvlDepth(Node *currentNode, int currentLevel=0) takes the currentNode and the currentLevel. The currentLevel has the default value of 0 if no value passed to it. If the currentNode is null then the function returns 0.

int deepestOddLvlDepth(Node *currentNode, int currentLevel=0){
   if ( currentNode == NULL)
      return 0;

The currentLevel is incremented by 1 on each recursion level till the base condition is met. We then check if the currentNode is an odd leaf node. The left and rightChild are then traversed till we find our deepest odd level leaf node depth. The max of leftChildDepth and rightChild depth are returned to the main function for printing the result.

int deepestOddLvlDepth(Node *currentNode, int currentLevel=0){
   if ( currentNode == NULL)
      return 0;
      currentLevel ++;
   if ( currentLevel % 2 != 0 && isLeaf(currentNode))
      return currentLevel;
   int leftChildLevel = deepestOddLvlDepth(currentNode->leftChild,currentLevel);
   int rightChildLevel = deepestOddLvlDepth(currentNode->rightChild,currentLevel);
   return max(leftChildLevel,rightChildLevel);
}

Example

Let us look at the following implementation to find the deepest odd level node depth in binary tree.

 Live Demo

#include<iostream>
using namespace std;
struct Node{
   int key;
   struct Node *leftChild, *rightChild;
};
Node* createNode(int key){
   Node* node = new Node;
   node->key = key;
   node->leftChild = node->rightChild = NULL;
   return node;
}
bool isLeaf(Node *currentNode){
   return (currentNode->leftChild == NULL &&
   currentNode->rightChild == NULL);
}
int deepestOddLvlDepth(Node *currentNode, int currentLevel=0){
   if ( currentNode == NULL)
      return 0;
      currentLevel ++;
   if ( currentLevel % 2 != 0 && isLeaf(currentNode))
      return currentLevel;
      int leftChildLevel = deepestOddLvlDepth(currentNode->leftChild,currentLevel);
      int rightChildLevel = deepestOddLvlDepth(currentNode->rightChild,currentLevel);
      return max(leftChildLevel,rightChildLevel);
}
int main(){
   Node *root = createNode(15);
   root->leftChild = createNode(33);
   root->rightChild = createNode(18);
   root->rightChild->leftChild = createNode(19);
   root->rightChild->rightChild = createNode(20);
   root->rightChild->rightChild->leftChild = createNode(28);
   root->rightChild->rightChild->rightChild = createNode(29);
   cout << "The depth of the deepest odd level leaf node is: "<<deepestOddLvlDepth(root) << endl;
   return 0;
}

Output

The above code will produce the following output.

The depth of the deepest odd level leaf node is: 3
raja
Published on 16-Jan-2021 08:13:18
Advertisements