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
9d

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!
Add answer anonymously...
Dunzo Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter