Trees · Coding problem
Mirror Tree
A free coding problem from the Binary Tree Mirror & Symmetry skill. Read it here, then solve it in the app in Java or Python.
- Easy
- Coding problem
- Free
- Java and Python
The problem
Given the root of a binary tree, transform it in place into its mirror image, where every node's left and right children are swapped.
Input format
The tree in the usual heap-array format.
Output format
Print the mirrored tree using the same level-order (BFS) serialization described in the tree-reconstruction questions: dequeue a node, print its value or null; a non-null node enqueues its two children, a dequeued null is never expanded further.
Example
Input:
7
1 2 3 4 5 6 7
Output: 1 3 2 7 6 5 4 null null null null null null null null
Constraints
0 <= number of nodes <= 1000-1000 <= node value <= 1000
How it's graded
- Run checks your code against the examples. Submit also runs a hidden suite in four groups - basic cases, edge cases, random inputs and performance tests at the size limit - and shows which groups pass, never the hidden inputs.
- Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
- Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.
More problems in Binary Tree Mirror & Symmetry
- Symmetric TreeFree
Solve it Mirror Tree free
No card needed. Lessons, practice questions and easy problems are free forever.
Free plan, no card · 7-day Pro trial · Cancel anytime