CodePark/ problems / Mirror a Binary Tree
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Trees: depth-first and breadth-first

Mirror a Binary Tree

easyabout 10 minutes in an interview

Given the root of a binary tree, turn it into its mirror image. At every node, the left and right children swap places. Return the root of the mirrored tree.

  • You may change the tree in place and return the same root.
  • An empty tree (null) stays null.
  • The tree has up to 1,000 nodes.
Input: root = [4, 2, 7, 1, 3, 6, 9] Output: [4, 7, 2, 9, 6, 3, 1]
Input: root = [1, 2] Output: [1, null, 2]The only child moves from the left side to the right side.
Input: root = [] Output: []