Loading...
Given the root of a binary tree, mirror it by swapping the left and right children of every node. Return the root of the mirrored tree.
Your function receives root as a TreeNode (or the empty tree, as null) and returns the root of the mirrored tree. The node type is provided for you, with val, left and right fields; you may mirror in place and return the same root.
The examples below write trees as their level-order traversal, where null marks a missing child of a listed node and trailing nulls are omitted. That is only how trees are displayed; the encoding and decoding are done for you.
Node.val ≤100The children of every node are swapped: 2 and 7 trade places under the root, 1 and 3 trade places under 2, and 6 and 9 under 7.
The empty tree mirrors to itself.
Click "Run" to test with sample cases or "Submit" to run all tests.