Find a mirror image of a binary tree

By | July 15, 2020

In this post, we will learn How to write a java program to find a mirror image of a binary tree?

To implement the algorithm to find the mirror image of a given binary tree in Java. A mirror image of a binary tree is another binary tree with left and right children of all non-leaf nodes of the given binary tree are interchanged.

Below are the images that show the given Binary Tree & mirror image of a given binary tree.

Algorithm Implementation

The left child of any node in the given tree will be the right child of the node in the mirror image. Below is the implementation for the same. The PreOrdeer traversal is just to validate the mirrored binary tree.

We will be going to use a recursive approach to find the mirror image of a given binary tree.

Mirror Image of Binary Tree

PreOrder Output of Given Binary Tree: 1 2 4 5 3 6 7                                            PreOrder Mirror Image of Binary Tree: 1 3 7 6 2 5 4

Below is the Complete Source code: 

BinaryTree.java

 

ClientTest.java

The output of This Program:

Binary Tree PreOrder Processing output….
1 2 4 5 3 6 7
Binary Tree PreOrder Processing output after converting into it’s Mirror Image….
1 3 7 6 2 5 4

You May Also Like:

Introduction to Tree Data Structure
Introduction to Binary Tree
Structure of Binary Trees
Operations and use of Binary Trees
Insert operation in a binary search tree
Delete operation in a binary search tree
Search operation in a binary search tree
Binary Tree Traversals
PreOrder traversal of binary tree implementation in Java
InOrder traversal of binary tree implementation in Java
PostOrder traversal of binary tree implementation in Java
Find the node with minimum and maximum values in a Binary Search Tree

That’s all about How to write a java program to find a mirror image of a binary tree?
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

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