Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Convert binary tree to mirror tree
EASY
15 mins
17 upvotes
Stacks & Queues
Trees
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Stacks & Queues
-
-
Trees
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
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
userLevel
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
yellow-spark
0/40
Avg time to solve 15 mins
Success Rate 85 %
Share
17 upvotes

Problem Statement

Given a binary tree, convert this binary tree into its mirror tree.

A binary tree is a tree in which each parent node has at most two children.

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.

alt text

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:

alt text

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
Reset Code
Full screen
Auto
copy-code
Console