Print Level of All nodes in a Binary Tree

• Write a program in C to print level of all nodes in a binary tree.
• How to print level of nodes of binary tree using recursion.

Given a binary tree, we have to print the level of all nodes of a binary tree. We define the level of a node as the number of parent nodes a node has.
The root of the tree, therefore, is at level 0. Root's children are at level 1. We will traverse each node of binary tree using pre order traversal and keep track of level of each node. Whenever we access any node during traversal, we will print level of current node.

Algorithm to print level of all nodes of binary tree
Let "root" be the pointer to the root node of binary tree. Our algorithm is based on the fact that, level of a child node is one more than level of parent node.
• Traverse binary tree using pre Order traversal. We will also pass current level of node as parameter in every recursive call.
• If root is equal to NULL, return.
• Print level of current node.
• Recursively print the level of nodes in left and right sub tree by incrementing level.
Time Complexity : O(n)

In this program, we will use a recursive function "printLevelofAllNode", which takes a node pointer and it's level as input and print the levels of all nodes n this sub tree by implementing above algorithm.

```void printLevelofAllNode(struct node* root, int currentLevel) {

if(root == NULL) {
return;
}

printf("Level of %d is %d \n", root->data, currentLevel);

printLevelofAllNode(root->left, currentLevel+1);
printLevelofAllNode(root->right, currentLevel+1);
}
```

C program to print level of all nodes of binary tree

```#include <stdio.h>

struct node {
int data;
struct node *left;
struct node *right;
};

struct node* getNewNode(int data) {
/* dynamically allocate memory for a new node */
struct node* newNode = (struct node*)malloc(sizeof(struct node));

/* populate data in new Node */
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}

/*
This function returns below tree
1
/ \
2    3
/ \    \
4  5    7
/ \
8  9
*/
struct node* generateBTree(){
// Root Node
struct node* root =  getNewNode(1);

root->left = getNewNode(2);
root->right = getNewNode(3);

root->left->left = getNewNode(4);
root->left->right = getNewNode(5);
root->right->right = getNewNode(7);

root->left->left->left = getNewNode(8);
root->left->left->right = getNewNode(9);

return root;
}

/*
Prints level of all nodes. It does pre Order
traversal and keeps track of the current level and prints it.
*/
void printLevelofAllNode(struct node* root, int currentLevel) {

if(root == NULL) {
return;
}

printf("Level of %d is %d \n", root->data, currentLevel);

printLevelofAllNode(root->left, currentLevel+1);
printLevelofAllNode(root->right, currentLevel+1);
}

int main() {
struct node *root = generateBTree();

/*Printing levels of all nodes */
printLevelofAllNode(root, 0);

getchar();
return 0;
}
```
Output
```Level of 1 is 0
Level of 2 is 1
Level of 4 is 2
Level of 8 is 3
Level of 9 is 3
Level of 5 is 2
Level of 3 is 1
Level of 7 is 2
```