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.

**Approach**:

- Insert all the connecting nodes to a 2-D vector tree.
- Run a BFS on the tree such that height[node] = 1 + height[parent]
- Once BFS 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:

*chevron_right*

*filter_none*

**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)

## Recommended Posts:

- Print levels with odd number of nodes and even number of nodes
- Print the nodes at odd levels of a tree
- Print all nodes between two given levels in Binary Tree
- Print Levels of all nodes in a Binary Tree
- Print Binary Tree levels in sorted order
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Print all nodes that don't have sibling
- Print Leaf Nodes at a given Level
- Print nodes at k distance from root
- Print all nodes at distance k from a given node
- Print all internal nodes of a Binary tree
- Print nodes at k distance from root | Iterative
- Print path between any two nodes in a Binary Tree
- Print Nodes in Top View of Binary Tree
- Print nodes in the Top View of Binary Tree | Set 3