# Print levels with odd number of nodes and even number of nodes

Given an N-ary tree, print all the levels with odd and even number of nodes in it.

Examples:

```For example consider the following tree
1               - Level 1
/     \
2       3           - Level 2
/   \       \
4     5       6        - Level 3
/  \     /
7    8   9         - Level 4

The levels with odd number of nodes are: 1 3 4
The levels with even number of nodes are: 2
```

Note: The level numbers starts from 1. That is, the root node is at the level 1.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Insert all the connecting nodes to a 2-D vector tree.
• Run a DFS on the tree such that height[node] = 1 + height[parent]
• Once DFS traversal is completed, increase the count[] array by 1, for every node’s level.
• Iterate from first level to last level, and print all nodes with count[] values as odd to get level with odd number nodes.
• Iterate from first level to last level, and print all nodes with count[] values as even to get level with even number nodes.

Below is the implementation of the above approach:

## C++

 `// C++ program to print all levels ` `// with odd and even number of nodes ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function for DFS in a tree ` `void` `dfs(``int` `node, ``int` `parent, ``int` `height[], ``int` `vis[], ` `         ``vector<``int``> tree[]) ` `{ ` `    ``// calculate the level of every node ` `    ``height[node] = 1 + height[parent]; ` ` `  `    ``// mark every node as visited ` `    ``vis[node] = 1; ` ` `  `    ``// iterate in the subtree ` `    ``for` `(``auto` `it : tree[node]) { ` ` `  `        ``// if the node is not visited ` `        ``if` `(!vis[it]) { ` ` `  `            ``// call the dfs function ` `            ``dfs(it, node, height, vis, tree); ` `        ``} ` `    ``} ` `} ` ` `  `// Function to insert edges ` `void` `insertEdges(``int` `x, ``int` `y, vector<``int``> tree[]) ` `{ ` `    ``tree[x].push_back(y); ` `    ``tree[y].push_back(x); ` `} ` ` `  `// Function to print all levels ` `void` `printLevelsOddEven(``int` `N, ``int` `vis[], ``int` `height[]) ` `{ ` `    ``int` `mark[N + 1]; ` `    ``memset``(mark, 0, ``sizeof` `mark); ` ` `  `    ``int` `maxLevel = 0; ` `    ``for` `(``int` `i = 1; i <= N; i++) { ` ` `  `        ``// count number of nodes ` `        ``// in every level ` `        ``if` `(vis[i]) ` `            ``mark[height[i]]++; ` ` `  `        ``// find the maximum height of tree ` `        ``maxLevel = max(height[i], maxLevel); ` `    ``} ` ` `  `    ``// print odd number of nodes ` `    ``cout << ``"The levels with odd number of nodes are: "``; ` `    ``for` `(``int` `i = 1; i <= maxLevel; i++) { ` `        ``if` `(mark[i] % 2) ` `            ``cout << i << ``" "``; ` `    ``} ` ` `  `    ``// print even number of nodes ` `    ``cout << ``"\nThe levels with even number of nodes are: "``; ` `    ``for` `(``int` `i = 1; i <= maxLevel; i++) { ` `        ``if` `(mark[i] % 2 == 0) ` `            ``cout << i << ``" "``; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Construct the tree ` ` `  `    ``/*   1 ` `       ``/   \ ` `      ``2     3 ` `     ``/ \     \ ` `    ``4    5    6 ` `        ``/ \  / ` `       ``7   8 9  */` ` `  `    ``const` `int` `N = 9; ` ` `  `    ``vector<``int``> tree[N + 1]; ` ` `  `    ``insertEdges(1, 2, tree); ` `    ``insertEdges(1, 3, tree); ` `    ``insertEdges(2, 4, tree); ` `    ``insertEdges(2, 5, tree); ` `    ``insertEdges(5, 7, tree); ` `    ``insertEdges(5, 8, tree); ` `    ``insertEdges(3, 6, tree); ` `    ``insertEdges(6, 9, tree); ` ` `  `    ``int` `height[N + 1]; ` `    ``int` `vis[N + 1] = { 0 }; ` ` `  `    ``height = 0; ` ` `  `    ``// call the dfs function ` `    ``dfs(1, 0, height, vis, tree); ` ` `  `    ``// Function to print ` `    ``printLevelsOddEven(N, vis, height); ` ` `  `    ``return` `0; ` `} `

## Python3

 `# Python3 program to print all levels  ` `# with odd and even number of nodes  ` ` `  `# Function for DFS in a tree  ` `def` `dfs(node, parent, height, vis, tree):  ` ` `  `    ``# calculate the level of every node  ` `    ``height[node] ``=` `1` `+` `height[parent]  ` ` `  `    ``# mark every node as visited  ` `    ``vis[node] ``=` `1` ` `  `    ``# iterate in the subtree  ` `    ``for` `it ``in` `tree[node]:  ` ` `  `        ``# if the node is not visited  ` `        ``if` `not` `vis[it]:  ` ` `  `            ``# call the dfs function  ` `            ``dfs(it, node, height, vis, tree)  ` `         `  `# Function to insert edges  ` `def` `insertEdges(x, y, tree):  ` ` `  `    ``tree[x].append(y)  ` `    ``tree[y].append(x)  ` ` `  `# Function to print all levels  ` `def` `printLevelsOddEven(N, vis, height):  ` ` `  `    ``mark ``=` `[``0``] ``*` `(N ``+` `1``)  ` `     `  `    ``maxLevel ``=` `0` `    ``for` `i ``in` `range``(``1``, N ``+` `1``):  ` ` `  `        ``# count number of nodes in every level  ` `        ``if` `vis[i]:  ` `            ``mark[height[i]] ``+``=` `1` ` `  `        ``# find the maximum height of tree  ` `        ``maxLevel ``=` `max``(height[i], maxLevel)  ` `     `  `    ``# print odd number of nodes  ` `    ``print``(``"The levels with odd number"``, ` `          ``"of nodes are: "``, end ``=` `"")  ` `    ``for` `i ``in` `range``(``1``, maxLevel ``+` `1``):  ` `        ``if` `mark[i] ``%` `2``:  ` `            ``print``(i, end ``=` `" "``)  ` `     `  `    ``# print even number of nodes  ` `    ``print``(``"\nThe levels with even number"``,  ` `          ``"of nodes are: "``, end ``=` `"")  ` `    ``for` `i ``in` `range``(``1``, maxLevel ``+` `1``):  ` `        ``if` `mark[i] ``%` `2` `=``=` `0``:  ` `            ``print``(i, end ``=` `" "``)  ` ` `  `# Driver Code  ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``# Construct the tree ` `    ``N ``=` `9` `    ``tree ``=` `[[] ``for` `i ``in` `range``(N ``+` `1``)]  ` ` `  `    ``insertEdges(``1``, ``2``, tree)  ` `    ``insertEdges(``1``, ``3``, tree)  ` `    ``insertEdges(``2``, ``4``, tree)  ` `    ``insertEdges(``2``, ``5``, tree)  ` `    ``insertEdges(``5``, ``7``, tree)  ` `    ``insertEdges(``5``, ``8``, tree)  ` `    ``insertEdges(``3``, ``6``, tree)  ` `    ``insertEdges(``6``, ``9``, tree)  ` ` `  `    ``height ``=` `[``0``] ``*` `(N ``+` `1``) ` `    ``vis ``=` `[``0``] ``*` `(N ``+` `1``)  ` ` `  `    ``# call the dfs function  ` `    ``dfs(``1``, ``0``, height, vis, tree)  ` ` `  `    ``# Function to print  ` `    ``printLevelsOddEven(N, vis, height)  ` ` `  `# This code is contributed by Rituraj Jain `

Output:

```The levels with odd number of nodes are: 1 3 4
The levels with even number of nodes are: 2
```

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : rituraj_jain

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.