Tree Inversion

0

0 votes
Easy
Problem

Construct a tree as in question 1. Then invert the tree i.e. for each node left chile becomes the right child and right child becomes the left child. Dio not construct a new tree, this has to be done in-place i.e. in the same constructed tree.

Input Format:

First line of input contains an integer n i.e. number of nodes. Second line contains n space separate integers.

Output Format:

After inversion, print the tree in inorder, preorder and postorder fashion. Print a newline after each traversal.

Constraints:

All the numbers are well with in the integer range.

Sample Input
5
1 4 4 5 6
3
9
5
10
Sample Output
6 5 4 4 1 
1 4 5 6 4 
6 5 4 4 1 
Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

Constructed Tree:

                      1
                        \
                        4
                       /  \
                      4    5
                            \ 
                             6

Tree after inversion:

                           1
                         /
                        4
                       /  \
                      5    4
                      /
                    6

Editor Image

?