VOOZH about

URL: https://www.geeksforgeeks.org/dsa/zigzag-level-order-traversal-of-an-n-ary-tree/

⇱ ZigZag Level Order Traversal of an N-ary Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

ZigZag Level Order Traversal of an N-ary Tree

Last Updated : 23 Jul, 2025

Given a Generic Tree consisting of n nodes, the task is to find the ZigZag Level Order Traversal of the given tree.
Note: A generic tree is a tree where each node can have zero or more children nodes. Unlike a binary tree, which has at most two children per node (left and right), a generic tree allows for multiple branches or children for each node.

Examples:

Input:

👁 second-largest-element-in-n-ary-tree-1


Output:
11
90 29 21
18 10 12 77

Approach:

The idea is to solve the given problem by using BFS Traversal. The approach is very similar to the Level Order Traversal of the N-ary Tree. It can be observed that on reversing the order of the even levels during the Level Order Traversal, the obtained sequence is a ZigZag traversal.

Below is the implementation of the above approach:


Output
11 
90 29 21 
18 10 12 77 

Time Complexity: O(n), where n is the number of nodes in the tree, as each node is processed once during the traversal.
Auxiliary Space: O(n), where n is the maximum number of nodes at any level in the tree, which is required for the queue and result storage.

Comment