![]() |
Given the root node of an N-ary tree and an integer K, the task is to convert the given tree into adjacency list representation and print the level order traversal considering vertex K as the root node. Example:
Approach: The given problem can be solved by using the DFS Traversal on the N-ary tree and storing the relation of all the edges into an adjacency list according to the adjacency list representation. The created adjacency list can be used to print the Level Order Traversal with K as the root node. This can be done using BFS traversal which is discussed in this article. Below is the implementation of the above approach: C++
Java
Python
C#
Javascript
Output:
5 1 9 10 11 2 3 4 6 7 8 Time Complexity: O(N) |
Reffered: https://www.geeksforgeeks.org
Tree |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |