Convert Binary Tree to Mirror Tree 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. The mirror of a binary tree T is another binary tree M(T) where the left and right children of all non-leaf nodes are interchanged.

Input:

The first line of input contains an integer ‘T’ denoting the number of test cases. The elements of the tree are given in level order form separated by a single space. If any node does not have a left or right child, use -1 in its place. 

Output:

For every test case, the inorder traversal of the mirror tree will be printed on a separate line with the elements of the mirror tree in level order form separated by a single space, using -1 in place of NULL left or right child.

Example:

Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Explanation:

Level order input represents a tree like below:

       1
/ \
2 3
/ / \
4 5 6
\
7

The mirror tree would be:

       1
/ \
3 2
/ \ \
6 5 4
\
7

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 3000
  • -10^9 ≤ DATA ≤ 10^9

where ‘N’ is the number of nodes in the tree, and 'DATA' denotes data contained in the node of a binary tree.

Note: You don't need to print anything; just implement the function to convert the given binary tree to its mirror tree in place.

AnswerBot
4mo

Convert a binary tree into its mirror tree by interchanging left and right children of non-leaf nodes.

  • Traverse the binary tree in a recursive manner and swap the left and right children of each non-le...read more

Help your peers!
Select
Add answer anonymously...

Dunzo Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Asteroid Collision Problem Description Given an array/list ASTEROIDS representin...read more
A Software Developer Intern was asked Q. Word Break II Problem Statement Given a non-empty string 'S' containing no space...read more
A Software Developer Intern was asked Q. Valid Parentheses Problem Statement Given a string 'STR' consisting solely of th...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Asteroid Collision Problem Description Given an array/list ASTEROIDS representin...read more
A Software Developer Intern was asked Q2. Word Break II Problem Statement Given a non-empty string 'S' containing no space...read more
A Software Developer Intern was asked Q3. Valid Parentheses Problem Statement Given a string 'STR' consisting solely of th...read more
Dunzo Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits