-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mirror_of_binary_tree.cpp
86 lines (71 loc) · 1.47 KB
/
Mirror_of_binary_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
Problem Statement:
-----------------
Given a binary tree, the task is to create a new binary tree which is a mirror image of the given binary tree.
Examples:
--------
Input:
5
/ \
3 6
/ \
2 4
Output:
Inorder of original tree: 2 3 4 5 6
Inorder of mirror tree: 6 5 4 3 2
Mirror tree will be:
5
/ \
6 3
/ \
4 2
*/
// Link --> https://www.geeksforgeeks.org/create-a-mirror-tree-from-the-given-binary-tree/
// Code:
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node* left;
Node* right;
}root;
Node* createNode(int val)
{
Node* newNode = new Node;
newNode->val = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void inorder(Node* root)
{
if(root == NULL)
return;
inorder(root->left);
cout<<root->val<<" ";
inorder(root->right);
}
void mirror(Node* root , Node** mirrortree)
{
if(root == NULL)
return;
*mirrortree = createNode(root->val);
mirror(root->left , &((*mirrortree)->right));
mirror(root->right , &((*mirrortree)->left));
}
int main()
{
Node* tree = createNode(5);
tree->left = createNode(3);
tree->right = createNode(6);
tree->left->left = createNode(2);
tree->left->right = createNode(4);
cout<<"Inorder of original tree: ";
inorder(tree);
Node* mirrortree = NULL;
mirror(tree , &mirrortree);
cout<<"\nInorder of mirror tree: ";
inorder(mirrortree);
return 0;
}