fork download
  1. //
  2. // main.cpp
  3. // Mirror Images Recursive
  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.  
  31. bool checkMirrorImages (Node *root1, Node *root2) {
  32. if (root1 == NULL && root2 == NULL) {
  33. return true;
  34. } else if (root1 == NULL || root2 == NULL) {
  35. return false;
  36. }
  37.  
  38. if (root1->info == root2->info && checkMirrorImages(root1->left, root2->right)
  39. && checkMirrorImages(root1->right, root2->left)) {
  40. return true;
  41. } else {
  42. return false;
  43. }
  44.  
  45. }
  46.  
  47.  
  48.  
  49. int main() {
  50. Node *root1 = newNode(1);
  51. root1->left = newNode(20);
  52. root1->right = newNode(21);
  53. root1->right->left = newNode(30);
  54. root1->right->right = newNode(31);
  55.  
  56. Node *root2 = newNode(1);
  57. root2->left = newNode(21);
  58. root2->right = newNode(20);
  59. root2->left->left = newNode(30);
  60. root2->left->right = newNode(31);
  61.  
  62.  
  63. if (checkMirrorImages(root1, root2)) {
  64. cout<<"Trees are mirror Images"<<endl;
  65. } else {
  66. cout<<"Trees are not mirror Images"<<endl;
  67. }
  68.  
  69. return 0;
  70. }
  71.  
Success #stdin #stdout 0.01s 5440KB
stdin
Standard input is empty
stdout
Trees are not mirror Images