FastPrepMirror a Binary Tree
Problems/Mirror a Binary Tree

Mirror a Binary Tree

AMD logoAMD● EasyFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source asks for the mirror image of a tree. In-place permission, empty behavior, serialization, bounds and interface are authored; approximate source match95%.

Return the mirror image of the binary tree by swapping the left and right children at every node. Preserve every node value. You may modify the input tree in place. An empty tree remains empty.

Function

mirrorTree(root: TreeNode) → TreeNode

Examples

Example 1

root = [4,2,7,1,3,6,9]return = [4,7,2,9,6,3,1]

The root children become7 and2; the next level becomes9,6,3,1.

Example 2

root = [1,2,null,3]return = [1,null,2,null,3]

Every left-only edge becomes a right-only edge.

Constraints

  • 0 <= serialized tree length <= 400.
  • -1000 <= every non-null node value <= 1000.
  • The input is a valid acyclic binary tree serialized level by level, with null for a missing child; [] denotes an empty tree.

More AMD problems

  • Insert After a Linked-List NodeONSITE INTERVIEW · Seen Oct 2026
  • LRU CacheONSITE INTERVIEW · Seen Oct 2026
  • Lowest Common Ancestor of a Binary TreePHONE SCREEN · Seen Oct 2026
All AMD interview questionsSee AMD hiring insights
Saved
public TreeNode mirrorTree(TreeNode root) {
    // Write your code here.
}
Checking account…