/** * [sameTree description] * @param a [description] * @param b [description] * @return [description] */ int sameTree(struct node* a, struct node* b) { // int retval_left = 0; // int retval_right = 0; if(a == NULL && b == NULL) { return 1; } else if(a == NULL || b == NULL) { return 0; } else { if(a->data == b->data) { return 1 && sameTree(a->left,b->left) && sameTree(a->right,b->right); } else { return 0; } } }
main() { struct node *a = Build123_a(); struct node *b = Build123_a(); struct node *c = Build123_b(); printf("a,b : %d\n",sameTree(a,b)); printf("a,c : %d\n",sameTree(a,c)); }
bool sameTree(struct node*a, struct node* b) { if (a == NULL && b == NULL) return true; else if (a != NULL && b != NULL) return (a->data == b->data) && sameTree(a->left, b->left) && sameTree(a->right, b->right); else return false; }
/* * Given two trees, return true if they are identical. */ bool sameTree(struct node* a, struct node* b){ if (NULL == a && NULL == b) return true; else if(a != NULL && b != NULL){ return a->data == b->data && sameTree(a->left, b->left) && sameTree(a->right, b->right); } else return false; }
int sameTree(node *a, node *b) { if(a == NULL && b == NULL) return(1); else if(a -> data == b -> data) return(1 && (sameTree(a -> left, b -> left) && sameTree(a -> right, b -> right))); else return(0); }
int sameTree(struct node *a, struct node *b) { if (a == 0 && b == 0) return 1; else if(a != 0 && b != 0) { return ((a->data == b->data) && (sameTree(a->left,b->left) && sameTree(a->right,b->right))); } else return 0; }
bool sameTree(struct treeNode *a, struct treeNode *b) { if((!a && b) || (a && !b)) {return FALSE;} if(!a && !b) {return TRUE;} if(a->val == b->val){ return(sameTree(a->left, b->left) && sameTree(a->right, b->right)); } else { return FALSE; } }
bool sameTree(TreeNode *r1, TreeNode *r2) { if (r1 == NULL) { if (r2 == NULL) { return true; } return false; } if (r2 == NULL) { return false; } if (r1->val != r2->val) { return false; } return sameTree(r1->left, r2->left) && sameTree(r1->right, r2->right); }
int main(void){ struct treeNode *a = NULL; struct treeNode *b = NULL; printf("%d\n", sameTree(a, b)); return 0; }
int sameTree(struct node* a, struct node* b) { if (a == NULL && b == NULL) { return 1; } if (a == NULL || b == NULL) { return 0; } if (a->data != b->data) { return 0; } return sameTree(a->left, b->left) && sameTree(a->right, b->right); }
/** * @param T1, T2: The roots of binary tree. * @return: True if T2 is a subtree of T1, or false. */ bool isSubtree(TreeNode *T1, TreeNode *T2) { if (sameTree(T1, T2)) { return true; } if (T1 == NULL) { return false; } return isSubtree(T1->left, T2) || isSubtree(T1->right, T2); }
int main(int argc, char* argv[]) { struct node* a = buildBSTree(); struct node* b = buildBSTree(); if (sameTree(a, b)) { printf("match\n"); } else { printf("no match\n"); } }
int main() { node *root = NULL, *rootb = NULL, *rooth = NULL, *temp; int sum = 25, *a, l = 0; a = (int *)malloc(sizeof(int)); root = insert(root, 10); root = insert(root, 7); root = insert(root, 6); root = insert(root, 8); root = insert(root, 13); root = insert(root, 15); root = insert(root, 11); printf("Part2: Size: \t%d\n", size(root, 0)); printf("Part3: Depth: \t%d\n", maxDepth(root)); printf("Part4: Min: \t%d\n", minValue(root)); printf("Part5: Post: \t");printPostorder(root);printf("\n"); printf("Part6: Path Sum: %d %s\n", sum, hasPathSum(root, sum)?"True":"False"); printf("Part7: Leaf Paths");printPaths(root, a, l); printf("\n"); inorder(root);printf("\n"); printf("Part8: Mirror: ");mirror(root);inorder(root);printf("\n"); rootb = insert(rootb, 10); rootb = insert(rootb, 7); rootb = insert(rootb, 6); rootb = insert(rootb, 8); rootb = insert(rootb, 13); rootb = insert(rootb, 15); rootb = insert(rootb, 11); printf("Part9: doubleTree: ");doubleTree(rootb);inorder(rootb);printf("\n"); printf("Part10: sameTree: %s\n", sameTree(root, root)?"True":"False"); mirror(root); printf("Part11: isBST: %s\n", isBST(root)?"True":"False"); printf("Part12: Tree list: "); rooth = treeList(root); temp = rooth -> left; printf("%d ", rooth -> data); while(temp != rooth) { printf("%d ", temp -> data); temp = temp -> left; } printf("\n"); return(0); }
int sameTree(struct node* a, struct node* b) { if(a != NULL || b != NULL) { if(a != NULL && b != NULL) { if(a->data != b->data) return 0; else { if(a->left == NULL && b->left == NULL && a->right == NULL && b->right == NULL) return 1; else { if(a->left != NULL || b->left != NULL ) { return sameTree(a->left,b->left); } if(a->right != NULL || b->right != NULL ) { return sameTree(a->right,b->right); } } } } else return 0; } }
int sameTree(struct node* a, struct node* b) { // initial state is true static int truth = 1; // check if nodes are NULL, if so check the other if (!a) { if (b) truth = 0; return truth; } else if (!b) { if (a) truth = 0; return truth; } // set truth to false if node data is not the same if (a->data != b->data) truth = 0; // recursively check the rest of the tree sameTree(a->left, b->left); sameTree(a->right, b->right); // return truth return truth; }
void testSameTree() { struct node* rootNode = insert(NULL,3); rootNode = insert(rootNode,4); rootNode = insert(rootNode,5); rootNode = insert(rootNode,2); rootNode = insert(rootNode,1); struct node* root = insert(NULL,3); root = insert(root,4); root = insert(root,5); root = insert(root,2); root = insert(root,0); if(sameTree(rootNode,root) == 1) printf("Trees are same \n"); else printf("Something went wrong \n"); }
int main() { struct node *root = NULL; root = insert(root, 4); root = insert(root, 2); root = insert(root, 3); root = insert(root, 1); root = insert(root, 5); root = mirror(root); std::cout << sameTree(root, root) << std::endl; std::cout << size(root) << std::endl; std::cout << maxDepth(root) << std::endl; printInoder(root); std::cout << std::endl; printPostorder(root); std::cout << std::endl; printPreorder(root); return 0; }