Thursday 2 August 2012

C function to determine whether the given two trees are identical or not

//where a and b are the pointer to the root nodes of the two trees

int identical(struct node* a, struct node* b) 

  if (a==NULL && b==NULL)

  {
    return 1;
  } 
  else if (a!=NULL && b!=NULL) 
  { 
  return (a->data == b->data && identical(a->left, b->left) && identical(a->right, b->right)); 
  } 
  else return 0; 
}


//if the return value is 1 then the trees are identical otherwise not

No comments:

Post a Comment