Invert Binary Tree

EasyTrees
Next
TreeDFSRecursion

Invert (mirror) a binary tree: swap the left and right child of every node. Print the inverted tree in preorder, writing null for each missing child. Build the tree from the level-order input yourself, then solve the problem.

Input

A single line giving the tree in level order, with the word null for a missing child (for example 3 9 20 null null 15 7).

Output

Print the inverted tree's preorder traversal (node, left subtree, right subtree), using null for a missing child, separated by spaces.

Constraints

1 <= number of nodes <= 1000

Example 1
Input
4 2 7 1 3 6 9
Output
4 7 9 null null 6 null null 2 3 null null 1 null null

Each level is mirrored: 4 has 7 on the left and 2 on the right, and so on.

Hints

Invert Binary Tree - Easy Trees Problem | Skill Shiksha