FastPrepMirror a Binary Tree
Problems/Mirror a Binary Tree

Mirror a Binary Tree

Adobe logoAdobe● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem reflects a binary tree from left to right, swapping the two sides at every node, and asks for both a recursive and a non-recursive version. An Adobe Computer Scientist 1 report gives this as the second coding task of its first round, says the interviewer wanted both versions, and asks for the mirrored tree to be printed; it gives no input format, examples or limits.

Our choices: return the root of the mirrored tree instead of printing it (the judge reads it level by level), at most 500 nodes with values from -1000 to 1000, and new examples. The judge checks only the returned tree, not which version you wrote.

The judged task matches the report at about 85%.

Given the root of a binary tree, reflect it from left to right: at every node, the left subtree and the right subtree trade places. Return the root of the mirrored tree.

You may change the given tree in place or build a new one. Write it both recursively and without recursion.

Function

mirrorTree(root: TreeNode) → TreeNode

Examples

Example 1

root = [8,3,10,1,6,null,14]return = [8,10,3,14,null,6,1]

At the root, 3 and 10 trade places. Under 10, the right child 14 becomes the left child. Under 3, 1 and 6 trade places. Read level by level, the result is [8, 10, 3, 14, null, 6, 1].

Example 2

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

A chain going left (1, then 2, then 3) becomes a chain going right.

Example 3

root = [5]return = [5]

A single node is its own mirror.

Constraints

  • The tree has between 1 and 500 nodes.
  • -1000 <= node.val <= 1000
  • The tree does not have to be balanced. It can be a single chain of 500 nodes.

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public TreeNode mirrorTree(TreeNode root) {
    // Write your code here.
}
Checking account…