Mirror Tree
easySave
Binary TreeDepth-First SearchRecursionTree
Given the root of a binary tree, transform the tree into its mirror image and return the root of the mirrored tree. A mirrored tree is a tree where the positions of the left and right children of all nodes are swapped.
Example 1
Input
[ 4, 2, 7, 1, 3, 6, 9 ]
Output
4, 7, 2, 9, 6, 3, 1
Explanation. Swap all left and right children in the tree.
Example 2
Input
[ 1, 2 ]
Output
1, null, 2
Explanation. The node 2, originally on the left, is now on the right.
Example 3
Input
[ 1 ]
Output
1
Explanation. A single node tree remains unchanged when mirrored.
Follow-up: Can you solve this problem both recursively and iteratively?
Constraints:
The tree will have at most 100 nodes. Each node's value is an integer.
- Views
- 3