868. Convert Binary Tree to its Mirror

EasyTreesBinary TreeDFSRecursion

Given the root of a binary tree, transform it into its mirror image by swapping the left and right children of every node, and return the resulting tree as a level-order array with null for missing children.

Input: A level-order array of the tree.

Output: Array — the mirrored tree in level order.

Examples

Example 1
Input: [1,2,3,4,5]
Output: [1,3,2,null,null,5,4]
Explanation: All children swapped.
Example 2
Input: [1]
Output: [1]
Explanation: Single node unchanged.
Example 3
Input: []
Output: []
Explanation: Empty tree.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →