341. Mirror the Tree
Swap every node's two children, top to bottom, then print the preorder of the result.
4 4
/ \ / \
2 6 -> 6 2 preorder becomes 4 6 7 5 2 3 1
/ \ / \ / \ / \
1 3 5 7 7 5 3 1
You do not have to modify anything. Mirroring and then walking in preorder is the same as walking the original in preorder while visiting the RIGHT child first, and that version needs no writes at all.
Both are accepted. Recognising that a transformation can sometimes be folded into the traversal is worth more than either solution on its own.
Constraints - `1 ≤ n ≤ 100000` - `-1000000000 ≤ value ≤ 1000000000` - The input always forms a valid tree rooted at node 1.
Input
The first line contains an integer n, the number of nodes.
The second line contains n values, where node i holds the ith value.
Each of the next n lines contains two integers, the left and right child of
node i, using 0 for no child. Node 1 is the root.
Output
Print the preorder of the mirrored tree, separated by single spaces.
Hints
Four rungs, in order. The last two open once you have submitted an attempt — a wrong one counts.