//
// main.cpp
// Mirror Images
//
// Created by Himanshu on 04/09/21.
//
#include <iostream>
#include <queue>
using namespace std;
const int N = 5;
struct node {
int info = 0;
struct node *left, *right;
};
typedef struct node Node;
node* newNode(int data)
{
node* Node = new node();
Node->info = data;
Node->left = NULL;
Node->right = NULL;
return(Node);
}
void printArray (int arr[]) {
for (int i=0; i<N; i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
}
bool checkMirrorImages (int A[], int B[]) {
for (int i=0, k=N-1; i<N && k>=0; i++, k--) {
if (A[i] != B[k]) {
return false;
}
}
return true;
}
void inorder (Node *root, int arr[], int &k) {
if (root == NULL) {
return;
}
// traverse left
inorder (root->left, arr, k);
// store root
arr[k++] = root->info;
// traverse right
inorder (root->right, arr, k);
}
int main() {
Node *root1 = newNode(1);
root1->left = newNode(20);
root1->right = newNode(21);
root1->right->left = newNode(30);
root1->right->right = newNode(31);
Node *root2 = newNode(1);
root2->left = newNode(21);
root2->right = newNode(20);
root2->left->left = newNode(31);
root2->left->right = newNode(30);
int A[N], B[N];
int k = 0;
inorder (root1, A, k);
k = 0;
inorder (root2, B, k);
cout<<"Array A:"<<endl;
printArray(A);
cout<<"Array B:"<<endl;
printArray(B);
if (checkMirrorImages(A, B)) {
cout<<"Trees are mirror Images"<<endl;
} else {
cout<<"Trees are not mirror Images"<<endl;
}
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBNaXJyb3IgSW1hZ2VzCi8vCi8vICBDcmVhdGVkIGJ5IEhpbWFuc2h1IG9uIDA0LzA5LzIxLgovLwoKI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8cXVldWU+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgTiA9IDU7CgpzdHJ1Y3Qgbm9kZSB7CiAgICBpbnQgaW5mbyA9IDA7CiAgICBzdHJ1Y3Qgbm9kZSAqbGVmdCwgKnJpZ2h0Owp9Owp0eXBlZGVmIHN0cnVjdCBub2RlIE5vZGU7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