Question: Given a binary tree, write a program to duplicate the tree (i.e. create an exact copy of the tree).
Time Complexity: O(n)
Time Complexity: O(n)
tree *
tree_duplicate(tree *t)
{
tree *tmp = NULL;
if (!t) {
return NULL;
}
tmp = (tree *) malloc (sizeof(tree));
if (!tmp) {
exit(ENOMEM);
}
tmp->data = t->data;
tmp->left = tree_duplicate(t->left);
tmp->right = tree_duplicate(t->right);
return (tmp);
}
No comments:
Post a Comment