Given a Tree where every node contains variable number of children, convert the tree to its mirror. Below diagram shows an example.
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.
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
Thanks to Nitin Agrawal for providing initial implementation. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Create a mirror tree from the given binary tree
- Convert a Binary Tree into its Mirror Tree
- Check mirror in n-ary tree
- Symmetric Tree (Mirror Image of itself)
- Find mirror of a given node in Binary tree
- Number of edges in mirror image of Complete binary tree
- Sum of the mirror image nodes of a complete binary tree in an inorder way
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Check if two trees are Mirror | Set 2
- Check if two trees are Mirror
- Iterative method to check if two trees are mirror of each other
- Check if two trees are mirror of each other using level order traversal
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)