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...
Top PubMatic SDE-2 interview questions & answers
Popular interview questions of SDE-2
Stay ahead in your career. Get AmbitionBox app
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
Get AmbitionBox app