I'm having a bit of bother deleting an entire tree. I can't seem to delete the root node and bcheck tells me I have leaks all over the place.
We've been given the structs:
typedef struct TNode {
int value;
struct TNode* left;
struct TNode* right;
}TNode;
typedef struct TNode* Tree;
and the prototype:
void destroyTree(Tree t);
Here's my code:
Code:void destroyTree(Tree t) { if(t->left != NULL) { destroyTree(t->left); t->left = NULL; free(t->left); } if(t->right != NULL) { destroyTree(t->right); t->right = NULL; free(t->right); } /* I added this to remove the root node, but I don't think it helps */ if(t->left == NULL && t->right == NULL) { t = NULL; free(t); } }