bool isMirror(TreeNode *a, TreeNode *b) {
    if (!a && !b)
      return true;
    if ((a && !b) || (!a && b) || (a->val != b->val))
      return false;

    return isMirror(a->left, b->right) && isMirror(a->right, b->left);
  }
 bool isMirror(TreeNode* a, TreeNode* b){
     if(!a && !b){
         return true;
     }
     if(a && b){
         return isMirror(a->left, b->right) && isMirror(a->right, b ->left) && a->val == b->val;
     }
     return false;
 }
 bool isMirror(TreeNode *node1, TreeNode *node2) {
     if (!node1 && !node2) return true;
     if (node1 && node2) {
         if (node1->val == node2->val) {
             return isMirror(node1->left, node2->right) && isMirror(node1->right, node2->left);
         }
     }
     
     return false;
 }
Beispiel #4
0
 bool isMirror(TreeNode * a, TreeNode * b) {
     
     if (a == NULL && b == NULL)
         return true;
     
     if (a && b && a->val == b->val) {
         return isMirror(a->left, b->right) && isMirror(a->right, b->left);
     }
     return false;
     
 }
Beispiel #5
0
 bool isMirror(TreeNode* left, TreeNode* right){
     if(left == NULL && right == NULL){
         return true;
     }
     if((left == NULL && right != NULL) || (left != NULL && right == NULL)){
         return false;
     }
     if(left->val == right->val){
         return isMirror(left->left, right->right) && isMirror(right->left, left->right);
     }
     else{
         return false;   
     }
 }
Beispiel #6
0
 bool isMirror(TreeNode* left, TreeNode* right) {
     if (left == NULL && right != NULL) {
         return false;
     }
     if (left != NULL && right == NULL) {
         return false;
     }
     if (left == NULL && right == NULL) {
         return true;
     }
     if (left->val != right->val) {
         return false;
     }
     return isMirror(left->left, right->right) && isMirror(left->right, right->left);
 }
Beispiel #7
0
bool isMirror(struct TreeNode* root1, struct TreeNode* root2) {
    if ((root1==NULL) && (root2==NULL))
        return true;
    if ((root1!=NULL) && (root2==NULL))
        return false;
    if ((root1==NULL) && (root2!=NULL))
        return false;
    if ((root1->val != root2->val))
        return false;
    if (!isMirror(root1->left, root2->right))
        return false;
    if (!isMirror(root1->right, root2->left))
        return false;
    return true;
}
Beispiel #8
0
    bool isSymmetric(TreeNode *root) {
        if (root == NULL) {
            return true;
        }

        return isMirror(root->left, root->right);
    }
Beispiel #9
0
bool isMirror(struct TreeNode *p, struct TreeNode *q) {
	if(NULL==p && NULL==q) return true;
    
	if(NULL!=p && NULL!=q){
		if(p->val==q->val &&
				isMirror(p->left,q->right) &&
				isMirror(p->right,q->left) ){
			return true;
		} else {
			return false;
		}

	}
	return false;
	
}
Beispiel #10
0
int main() {
  int isM, isP;
  char alpha_mirror[26] = {
    'A', ' ', ' ', ' ', '3',
    ' ', ' ', 'H', 'I', 'L', 
    ' ', 'J', 'M', ' ', 'O', 
    ' ', ' ', ' ', '2', 'T',
    'U', 'V', 'W', 'X', 'Y',
    '5'
  };

  char digit_mirror[10] = {
    '0', '1', 'S', 'E', ' ',
    'Z', ' ', ' ', '8', ' '
  };

  while (scanf("%s", str) != EOF) {
    isM = isMirror(str, alpha_mirror, digit_mirror);
    isP = isPa(str);

    if (!isM && !isP) printf ("%s -- is not a palindrome.\n", str);
    else if (!isM && isP) printf ("%s -- is a regular palindrome.\n", str);
    else if (isM && !isP) printf ("%s -- is a mirrored string.\n", str);
    else printf ("%s -- is a mirrored palindrome.\n", str);
    printf ("\n");
  }

  return 0;
}
Beispiel #11
0
 bool isSymmetric(TreeNode* root) {
 
     if (root == NULL)
         return true;
     
     if (isMirror(root, root))
         return true;
     return false;
 }
Beispiel #12
0
bool isSymmetric(struct TreeNode *root) {
	if(NULL==root||
			(NULL==root->right && NULL==root->left))
		return true;
    
	if(NULL!=root->right && NULL!=root->left){
		return isMirror(root->right,root->left);
	}
	return false;
}
Beispiel #13
0
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
int isMirror( TreeNode *root1,  TreeNode *root2)
{
    // If both trees are empty, then they are mirror images
    if (root1 == NULL && root2 == NULL)
        return 1;
 
    // For two trees to be mirror images, the following three
    // conditions must be true
    // 1 - Their root node's key must be same
    // 2 - left subtree of left tree and right subtree
    //      of right tree have to be mirror images
    // 3 - right subtree of left tree and left subtree
    //      of right tree have to be mirror images
    if (root1 && root2 && root1->val == root2->val)
        return isMirror(root1->left, root2->right) &&
               isMirror(root1->right, root2->left);
 
    // if neither of above conditions is true then root1
    // and root2 are not mirror images
    return 0;
}
Beispiel #14
0
bool isSymmetric(struct TreeNode* root) {
    if (root==NULL)
        return true;
    return isMirror(root->left, root->right);
}
Beispiel #15
0
 bool isMirror(TreeNode *l, TreeNode *r) {
     if (l == NULL && r == NULL) return true;
     if (l == NULL || r == NULL) return false;
     if (l->val != r->val) return false;
     return isMirror(l->left, r->right) && isMirror(l->right, r->left);
 }
  bool isSymmetric(TreeNode *root) {
    if (!root)
      return true;

    return isMirror(root->left, root->right);
  }
Beispiel #17
0
int Solution::isSymmetric(TreeNode* root) {
    
    return isMirror(root,root);
    
}
 bool isMirror(TreeNode *left, TreeNode *right){
     if(left == NULL || right == NULL)   return left == NULL && right == NULL;
     return left->val == right->val && isMirror(left->left, right->right) && isMirror(left->right, right->left);
 }
Beispiel #19
0
int main (int argc, const char * argv []) {
	
	printf("\n\tStart inserting into the tree...\n");
	TNODE_p_t tree = createNode();
	
	int choice;
	do {
		printf("\n---------------------------------------------------------------------");
		printf("\n\t0. Rebuild tree (recursive)\n\t1. Insert element (iterative)\n\t2. Insert element (recursive)\n\t3. Preorder transversal (recursive)\n\t4. Preorder transversal (iterative)\n\t5. Inorder transversal (recursive)\n\t6. Inorder transversal (iterative)\n\t7. Postorder transversal (recursive)\n\t8. Postorder transversal (iterative)\n\t9. Level order transversal (recursive)\n\t10. Search for an item (recursive)\n\t11. Check if it's BST.\n\t12. Check if it's a Mirror.\n\tEnter choice: ");
		scanf(" %d", &choice);
		
		String item = initString(SIZE);
		
		switch (choice) {
				
			case 0: tree = createNode();
				break;
				
			case 1: printf("\n\tEnter item to be inserted: ");
				scanf(" %s", item);
				insertIterative(&tree, item);
				break;
				
			case 2: printf("\n\tEnter item to be inserted: ");
				scanf(" %s", item);
				insertRecursive(&tree, item);
				break;
				
			case 3: printf("\n\tPreorder (Rec): ");
				preorderRecursive(tree);
				break;
				
			case 4: printf("\n\tPreorder (Iter): ");
				preorderIterative(tree);
				break;
				
			case 5: printf("\n\n\tInorder (Rec): ");
				inorderRecursive(tree);
				break;
				
			case 6: printf("\n\tInorder (Iter): ");
				inorderIterative(tree);
				break;
				
			case 7: printf("\n\n\tPostorder (Rec): ");
				postorderRecursive(tree);
				break;
				
			case 8: printf("\n\tPostorder (Iter): ");
				postorderIterative(tree);
				break;
				
			case 9: printf("\n\n\tLevel Order: ");
				levelOrder(tree);
				break;
				
			case 10: {
				printf("\tEnter item to be searched: ");
				scanf(" %s", item);
				TNODE_p_t loc = search(tree, item);
				if (loc != NULL)
					printf("\n\t'%s' is present in the tree. (%p)\n", item, loc);
				else
					printf("\n\t'%s' is not present in the tree.\n", item);
				break;
			}
				
			case 11: {
				if (isBST(tree)) {
					printf("\n\tTree is BST. Inorder: ");
					inorderRecursive(tree);
				}
				else {
					printf("\n\tTree is not a BST. Inorder: ");
					inorderRecursive(tree);
				}
				break;
			}
				
			case 12: {
				if (isMirror(tree)) {
					printf("\n\tTree is a mirror. Inorder: ");
					inorderRecursive(tree);
				}
				else {
					printf("\n\tTree is not a mirror. Inorder: ");
					inorderRecursive(tree);
				}
				break;
			}
				
			default: break;
				
		}
		
	} while (choice >= 0 && choice <= 12);
	
}
 bool isSymmetric(TreeNode *root) {
   return isMirror(root, root);
 }
 bool isMirror(TreeNode *t1, TreeNode *t2) {
   if (!t1 && !t2) return true;
   if (!t1 || !t2) return false;
   return t1->val == t2->val && isMirror(t1->left, t2->right) && isMirror(t1->right, t2->left);
 }