
- 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
Create a mirror tree from the given binary tree in C++ Program
In this tutorial, we are going to reflect the given binary tree.
Let's see the steps to solve the problem.
Write a struct node.
Create the binary tree with dummy data.
Write a recursive function to find the mirror of the given binary tree.
Recursively call the function with left and right nodes.
Swap the left node data with the right node data.
Print the tree.
Example
Let's see the code.
#include<bits/stdc++.h> using namespace std; struct Node { int data; struct Node* left; struct Node* right; }; struct Node* newNode(int data) { struct Node* node = new Node; node->data = data; node->left = NULL; node->right = NULL; return node; } void convertTreeToItsMirror(struct Node* node) { if (node == NULL) { return; } else { struct Node* temp; convertTreeToItsMirror(node->left); convertTreeToItsMirror(node->right); temp = node->left; node->left = node->right; node->right = temp; } } void printTree(struct Node* node) { if (node == NULL) { return; } printTree(node->left); cout << node->data << " "; printTree(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); cout << "Tree: "; printTree(root); cout << endl; convertTreeToItsMirror(root); cout << "Mirror of the Tree: "; printTree(root); cout << endl; return 0; }
Output
If you run the above code, then you will get the following result.
Tree: 4 2 5 1 3 Mirror of the Tree: 3 1 5 2 4
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Convert a Binary Tree into its Mirror Tree in C++
- Find mirror of a given node in Binary tree in C++
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- Program to pruning a given binary tree in C++
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- Construct Special Binary Tree from given Inorder traversal in C++
- C++ Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
- Deletion in a Binary Tree in C++ Program
- Program to print path from root to a given node in a binary tree using C++
- Program to find largest binary search subtree from a given tree in Python
- Binary Tree to Binary Search Tree Conversion in C++
- Construct a Binary Search Tree from given postorder in Python
- Find distance from root to given node in a binary tree in C++
- Construct Binary Tree from String in C++

Advertisements