Notice Period

Flip the Mobile

easy

A hanging mobile sculpture arrived from the workshop assembled backwards — every crossbar has its left and right arms swapped, all the way down. The fix is the same at every joint: swap the two arms, then fix each arm the same way.

Given the root of a binary tree, produce its mirror image: for every node, the left and right subtrees trade places. Return the root of the mirrored tree.

Tests pass the tree as a level-order array (null marks a missing child); build_tree and to_level_list handle the conversion — your work happens in _invert, on real nodes.

Example 1
in values = [4, 2, 7, 1, 3, 6, 9]
out [4, 7, 2, 9, 6, 3, 1]
Every node's children are swapped: 2 and 7 trade places, then their children do too.
Example 2
in values = [2, 1, 3]
out [2, 3, 1]
Example 3
in values = []
out []
An empty mobile is already its own mirror.
Constraints
  • · 0 <= number of nodes <= 100
  • · -100 <= node value <= 100
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.