Asked inPubMatic,SDE-2

Convert Binary Tree to Mirror Tree

Convert a given binary tree into its mirror tree, where the left and right children of all non-leaf nodes are interchanged.

Input:

An integer ‘T’ denoting the number of test cases. For each test case, the elements of the tree in level order form, separated by a single space. Use -1 in place of a null left or right child.

Output:

For each test case, the inorder traversal of the mirror tree, printed on a separate line.

Example:

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

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 3000
  • -10^9 <= DATA <= 10^9

Note: Modify the binary tree in place to get the mirror, without creating a new tree.

Be the first one to answer
Add answer anonymously...
PubMatic SDE-2 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