fixed bug in freeing memory

http://stackoverflow.com/questions/16138102/freeing-dynamically-allocated-memory-in-a-tree