Given a complete binary tree of depth H. If the mirror image from the left and the right side of this tree is taken then:
Right Mirrored Image: Rightmost node of the every level is connected to mirrored corresponding node.
Left Mirrored Image: Left most node of the every level is connected to mirrored corresponding node.
The task is to find the number of edges after taking both the mirror images in the final tree.
Input: H = 1
2 edges in the original tree will get mirrored in the mirror images (left and right) i.e. 6 edges in total.
And the edges connecting the mirror images with the original tree as shown in the image above.
Input: H = 2
(6 * 3) + 3 + 3 = 24
Approach: Maintain the leftmost, rightmost nodes after each mirror image. Number of edges will change after each operation of mirror image. Initially,
After right mirrored image:
After left mirrored image:
In complete modified tree:
Below is the implementation of the above approach:
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.
- Sum of the mirror image nodes of a complete binary tree in an inorder way
- Number of edges in a perfect binary tree with N levels
- Symmetric Tree (Mirror Image of itself)
- Convert a Binary Tree into its Mirror Tree
- Create a mirror tree from the given binary tree
- Find mirror of a given node in Binary tree
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Sideways traversal of a Complete Binary Tree
- Linked complete binary tree & its creation
- Find the largest Complete Subtree in a given Binary Tree
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Height of a complete binary tree (or Heap) with N nodes
- Iterative Boundary Traversal of Complete Binary tree
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Construct Complete Binary Tree from its Linked List Representation
- Print path from root to all nodes in a Complete Binary Tree
- Construct a complete binary tree from given array in level order fashion
- Check if value exists in level-order sorted complete binary tree
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.