
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Convert a Binary Tree into its Mirror Tree in C++
In this tutorial, we will be discussing a program to convert a binary tree into its mirror tree.
For this, we will be provided with a binary tree. Our task will be to swap the values on the left and the right end creating a mirror tree from the given binary tree.
Example
#include<bits/stdc++.h> using namespace std; //binary tree node structure struct Node{ int data; struct Node* left; struct Node* right; }; //creation of a new node with no child nodes struct Node* newNode(int data){ struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } void mirror(struct Node* node){ if (node == NULL) return; else{ struct Node* temp; //swapping the subtrees mirror(node->left); mirror(node->right); temp = node->left; node->left = node->right; node->right = temp; } } //printing the inorder traversal void print_tree(struct Node* node){ if (node == NULL) return; print_tree(node->left); cout << node->data << " "; print_tree(node->right); } int main(){ struct Node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); //printing the initial tree cout << "Inorder traversal of the constructed" << endl; print_tree(root); mirror(root); //printing the mirror tree cout << "\nInorder traversal of the mirror tree" << endl; print_tree(root); return 0; }
Output
Inorder traversal of the constructed 4 2 5 1 3 Inorder traversal of the mirror tree 3 1 5 2 4
- Related Articles
- Create a mirror tree from the given binary tree in C++ Program
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue) in C++
- Program to convert a linked list into a binary search tree in C++
- Insert into a Binary Search Tree in C++
- Find mirror of a given node in Binary tree in C++
- Convert Ternary Expression to a Binary Tree in C++
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property in C++
- Binary Tree to Binary Search Tree Conversion in C++
- Convert a given Binary tree to a tree that holds Logical AND property on C++
- Difference between Binary Tree and Binary Search Tree
- Convert Sorted List to Binary Search Tree in C++
- Convert Sorted Array to Binary Search Tree in Python
- Check if a binary tree is subtree of another binary tree in C++
- Binary Indexed Tree or Fenwick Tree in C++?
- Convert a Binary Tree to a Circular Doubly Link List in C++

Advertisements