Delete the Binary Tree

Objective: Given a binary tree, write an algorithm to delete it.

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

Approach:

  • To delete a binary tree, we need to set all the node objects to null then garbage collection will take care of rest of the things. If you are writing the code in C/C++ then you will have to clear the allocated memory by yourself.
  • Do the post order traversal and set the node to null.

Code:

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

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

%d bloggers like this: