• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
June 01, 2022 |6.0K Views

General Tree Level Order Traversa

Description
Discussion

Given a generic tree, perform a Level order traversal and print all of its nodes

The approach to this problem is similar to Level Order traversal in a binary tree. We Start with pushing root node in a queue and for each node we pop it, print it and push all its child in the queue.

In case of a generic tree we store child nodes in a vector. Thus we put all elements of the vector in the queue. 
Examples: 

Input :            10
            /   /    \   \
           2  34    56   100
          / \        |   / | \
         77  88      1   7  8  9

Output : 10
        2 34 56 100
        77 88 1 7 8 9

General Tree Level Order Traversal : https://www.geeksforgeeks.org/generic-tree-level-order-traversal/