Given a binary tree, invert the binary tree and return it.

Look at the example for more details.

Mirror of a Tree: Mirror of a Binary Tree T is another Binary Tree M(T) with left and right children of all non-leaf nodes interchanged.

Example 1:
Input: 1 / \ 3 2 / \ 4 5 Output: Invert and return 1 / \ 2 3 / \ 5 4
Example 2:
Input: 1 / \ 2 3 / \ / \ 4 5 6 7 Output: Invert and return 1 / \ 3 2 / \ / \ 7 6 5 4

Solution

Do tree traversal through the recursion, and swap the left and right pointers with each other.

Code

We encourage you to write a comment if you have a better solution or having any doubt on the above topic.

Leave a Reply

Your email address will not be published. Required fields are marked *