fork download
  1. //
  2. // main.cpp
  3. // Mirror Images
  4. //
  5. // Created by Himanshu on 04/09/21.
  6. //
  7.  
  8. #include <iostream>
  9. #include <queue>
  10. using namespace std;
  11.  
  12. const int N = 5;
  13.  
  14. struct node {
  15. int info = 0;
  16. struct node *left, *right;
  17. };
  18. typedef struct node Node;
  19.  
  20. node* newNode(int data)
  21. {
  22. node* Node = new node();
  23. Node->info = data;
  24. Node->left = NULL;
  25. Node->right = NULL;
  26.  
  27. return(Node);
  28. }
  29.  
  30. void printArray (int arr[]) {
  31. for (int i=0; i<N; i++) {
  32. cout<<arr[i]<<" ";
  33. }
  34. cout<<endl;
  35. }
  36.  
  37. bool checkMirrorImages (int A[], int B[]) {
  38.  
  39. for (int i=0, k=N-1; i<N && k>=0; i++, k--) {
  40. if (A[i] != B[k]) {
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46.  
  47. void inorder (Node *root, int arr[], int &k) {
  48. if (root == NULL) {
  49. return;
  50. }
  51.  
  52. // traverse left
  53. inorder (root->left, arr, k);
  54.  
  55. // store root
  56. arr[k++] = root->info;
  57.  
  58. // traverse right
  59. inorder (root->right, arr, k);
  60. }
  61.  
  62.  
  63.  
  64. int main() {
  65. Node *root1 = newNode(1);
  66. root1->left = newNode(20);
  67. root1->right = newNode(21);
  68. root1->right->left = newNode(30);
  69. root1->right->right = newNode(31);
  70.  
  71. Node *root2 = newNode(1);
  72. root2->left = newNode(21);
  73. root2->right = newNode(20);
  74. root2->left->left = newNode(31);
  75. root2->left->right = newNode(30);
  76.  
  77. int A[N], B[N];
  78. int k = 0;
  79.  
  80. inorder (root1, A, k);
  81. k = 0;
  82. inorder (root2, B, k);
  83.  
  84. cout<<"Array A:"<<endl;
  85. printArray(A);
  86. cout<<"Array B:"<<endl;
  87. printArray(B);
  88.  
  89. if (checkMirrorImages(A, B)) {
  90. cout<<"Trees are mirror Images"<<endl;
  91. } else {
  92. cout<<"Trees are not mirror Images"<<endl;
  93. }
  94. return 0;
  95. }
  96.  
Success #stdin #stdout 0s 5260KB
stdin
Standard input is empty
stdout
Array A:
20 1 30 21 31 
Array B:
31 21 30 1 20 
Trees are mirror Images