VOOZH about

URL: https://www.geeksforgeeks.org/dsa/mirror-of-n-ary-tree/

⇱ Mirror of n-ary Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Mirror of n-ary Tree

Last Updated : 13 Feb, 2023

Given a Tree where every node contains variable number of children, convert the tree to its mirror. Below diagram shows an example. 

👁 nAryMirror


We strongly recommend you to minimize your browser and try this yourself first. 
Node of tree is represented as a key and a variable sized array of children pointers. The idea is similar to mirror of Binary Tree. For every node, we first recur for all of its children and then reverse array of children pointers. We can also do these steps in other way, i.e., reverse array of children pointers first and then recur for children.

Below is C++ implementation of above idea. 


Output
Level order traversal Before Mirroring
10 
2 34 56 100 
1 7 8 9 

Level order traversal After Mirroring
10 
100 56 34 2 
9 8 7 1 

Time Complexity: O(N) here N is number of nodes

Auxiliary Space: O(N) ,here N is number of nodes

Comment
Article Tags:
Article Tags: