845. Mirror / Invert a Binary Tree

EasyTreesBinary TreeDFSRecursion

Given the root of a binary tree, invert it (swap 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 inverted tree in level order.

Examples

Example 1
Input: [4,2,7,1,3,6,9]
Output: [4,7,2,9,6,3,1]
Explanation: All left/right 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 →