Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Delete the Binary Tree

Objec­tive: Given a binary tree, write an algo­rithm to delete it.

This is one of the basic prob­lem in trees. if you are new to trees then this prob­lem will help you build your foundation.

Approach:

  • To delete a binary tree, we need to set all the node objects to null then garbage col­lec­tion will take care of rest of the things. If you are writ­ing the code in C/C++ then you will have to clear the allo­cated mem­ory by yourself.
  • Do the post order tra­ver­sal and set the node to null.

Code:

Output:
Deleting Node:4
Deleting Node:5
Deleting Node:2
Deleting Node:3
Deleting Node:1

You may also like...