Level Order Traversal of N-ary Tree
Given an N-ary Tree. The task is to print the level order traversal of the tree where each level will be in a new line.
Examples:
Input:
Image
Output:
1
3 2 4
5 6
Explanation: At level 1: only 1 is present.
At level 2: 3, 2, 4 is present
At level 3: 5, 6 is presentInput:
Image
Output:
1
2 3 4 5
6 7 8 9 10
11 12 13
14
Explanation: For the above example there are 5 level present in the n-ary tree.
At level 1: only 1 is present.
At level 2: 2, 3, 4, 5 is present.
At level 3: 6, 7, 8, 9, 10 is present
At level 4:11, 12, 13 is present
At level 5 :- 14 is present
Approach: The approach of the problem is to use Level Order Traversal and store all the levels in a 2D array where each of the levels is stored in a different row.
Follow the below steps to implement the approach:
- Create a vector ans and temp to store the level order traversal of the N-ary tree.
- Push the root node in the queue.
- Run a while loop until the queue is not empty:
- Determine the size of the current level which is the size of the queue (say N):
- Run a loop for i = 1 to N
- In each step delete the front node (say cur) and push its data to the temp as a part of the current level.
- Push all the children of cur into the queue.
- Push the temp into the final ans vector which stores the different levels in different rows.
- Determine the size of the current level which is the size of the queue (say N):
- Return the ans vector.
Below is the implementation of the above approach:
C++
// C++ code for above implementation#include <bits/stdc++.h>using namespace std; struct Node { char val; vector<Node*> children;}; // Utility function to create a new tree// nodeNode* newNode(int key){ Node* temp = new Node; temp->val = key; return temp;} // Function for level order traversal// for n-array treevector<vector<int> > levelOrder(Node* root){ vector<vector<int> > ans; if (!root) cout << "N-Ary tree does not any nodes"; // Create two queues main_queue // and child_queue queue<Node*> main_queue; // Push the root value in the main_queue main_queue.push(root); // Create a temp vector to store the // all the node values present at a // particular level vector<int> temp; // Run a while loop until the // main_queue is empty while (!main_queue.empty()) { // Get the front of the main_queue int n = main_queue.size(); // Iterate through the current level for (int i = 0; i < n; i++) { Node* cur = main_queue.front(); main_queue.pop(); temp.push_back(cur->val); for (auto u : cur->children) main_queue.push(u); } ans.push_back(temp); temp.clear(); } return ans;} // Driver codeint main(){ Node* root = newNode(1); root->children.push_back(newNode(3)); root->children.push_back(newNode(2)); root->children.push_back(newNode(4)); root->children[0]->children.push_back(newNode(5)); root->children[0]->children.push_back(newNode(6)); // LevelOrderTraversal obj; vector<vector<int> > ans = levelOrder(root); for (auto v : ans) { for (int x : v) cout << x << " "; cout << endl; } return 0;} |
1 3 2 4 5 6
Time Complexity: O(V) where V is the number of nodes
Auxiliary Space: O(V)



