Ctrl K

Mirror the Tree

Easy
You are given the root of a binary tree. Turn it into its mirror image: at every node, the left and right subtrees trade places. Return the root of the resulting tree. If the tree is empty, return None.
Example 1
root=tree_node([10, 5, 20, 3, None, 15]) → [10, 20, 5, None, 15, None, 3]

Every node swaps its left and right subtrees: 20 moves to the left, 5 to the right, and 3 and 15 switch sides under their parents.

Example 2
root=tree_node([1, None, 2]) → [1, 2]

The only right child becomes a left child.

Example 3
root=tree_node([]) → None

An empty tree has nothing to mirror.

Constraints
  • 0 ≤ number of nodes ≤ 100
  • -100 ≤ Node.val ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsIdentical Trees EasyMirror-Image Tree EasyTree Height EasyNearest Leaf Depth EasyLeaf Path With a Given Total Easy
loading Python…
Input
root=tree_node([10, 5, 20, 3, None, 15])
Expected
[10, 20, 5, None, 15, None, 3]

Run checks the first 3 tests in your browser. Submit checks all 72 tests on the server, hidden ones included.