Example: 1 / \ 2 3 / \ / \ 4 5 6 7 | V Mirror Form 1 / \ 3 2 / \ / \ 7 6 5 4 Output = Level Order:[1, 3, 2, 7, 6, 5, 4]
right node
at each level of the tree.
1. Create and initialize a temporary TreeNode
variable with null
.
2. If root
is not null
, recurse through its right nodes
and also through its left nodes
.
3. Swap the left node
with the right node
by using a temporary tree node
variable. In this case, since the data is not a complicated object, you could also just swap the data values of the nodes.
4. Return the root
node.
public TreeNode mirror(TreeNode root) { }
C
Java
Python