freeing memory of a binary tree C
Since it's a tree, you should go with a recursive approach.
deallocate (node):
//do nothing if passed a non-existent node
if node is null
return
//now onto the recursion
deallocate(left node)
deallocate(right node)
free node
Think about what the different traversal types do, and keep in mind, after you free memory you're not allowed to access it anymore:
- Preorder: operation performed before visiting any children
- In-order: operation performed after visiting left subtree, before right subtree
- Postorder: operation performed after visiting all subtrees
Given the above statements, the answer should be clear.