Close
Topic list
Convert binary tree to mirror tree
EASY
15 mins
Stacks & Queues
Trees
Topics (Covered in this problem)
Problem solved
Skill meter
Stacks & Queues
-
-
Trees
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Convert binary tree to mirror tree

Contributed by
Deep Mavani
Easy
0/40
Avg time to solve 15 mins
Success Rate 85 %
Share

Problem Statement

Mirror of a Tree: Mirror of a Binary Tree T is another Binary Tree M(T) with left and right children of all non-leaf nodes interchanged.

Note:
``````1. Make in-place changes, that is, modify the nodes given a binary tree to get the required mirror tree.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
``````2
1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1
1 3 2 -1 -1 5 4 -1 -1
``````
Sample output 1:
``````1 3 2 7 6 5 4 -1 -1 -1 -1 -1 -1 -1 -1
1 2 3 4 5 -1 -1 -1 -1 -1 -1
``````
Explanation to sample input 1:

``````In a mirror, objects on the left appear to be on the right, and objects on the right appear to be on the left.

Similarly, in the above figure,

At level 1: Since only the node with value 1 is present, it stays as at root in the mirror tree as well.

At level 2: Subtree with root 2 appears on the left and the subtree with root 3 appears on the right in the actual tree, their positions are swapped in the mirror tree. Now, subtree with root 3 comes on the left and subtree with root 2 on the right.

At level 3: Nodes 6 and 7 change positions and nodes 4 and 5 also change their positions.
``````
Sample Input 2:
``````1
10 12 6 -1 -1 15 4
``````
Sample output 2:
``````10 6 12 4 15 -1 -1 -1 -1 -1 -1
``````
Auto
Console