# Delete Binary Tree

“** Delete Binary Tree**” is an elementary problem based on tree data structure. Here, we are given a binary tree and our task is to delete given binary tree. We need to delete all the nodes and release memory.

If tree needs to be deleted, then, it needs to be traversed first and one-by-one memory of all the nodes must be released. It’s obvious that before deleting any parent node, we need to delete child node. So, for that, postorder traversal can be used, as it will help to delete left child, then right child and in last root node.

#### C++ Program to Delete Binary Tree is as follows:

```
/* C++ Program to Delete Binary Tree */
#include<bits/stdc++.h>
using namespace std;
typedef struct Node
{
int data;
struct Node *left;
struct Node *right;
Node(int ele)
{
data = ele;
left = NULL;
right = NULL;
}
} Node;
/* Function to Delete Binary Tree */
void deleteTree(Node *root)
{
/* Base Case */
if(root == NULL)
return;
deleteTree(root -> left);
deleteTree(root -> right);
cout<<"Deleting Node with Value "<<root->data<<"\n";
delete root;
}
int main()
{
/* Creating a Binary trees and inserting some nodes in it */
Node *root = NULL;
root = new Node(1);
root -> left = new Node(2);
root -> right = new Node(3);
root -> left -> left = new Node(4);
root -> left -> right = new Node(5);
root -> right -> left = new Node(6);
root -> right -> right = new Node (7);
root -> left -> right -> left = new Node(8);
root -> right -> left -> right = new Node(9);
/* Calling function to delete binary tree */
deleteTree(root);
}
```

**OUTPUT:**

Deleting Node with Value 4 Deleting Node with Value 8 Deleting Node with Value 5 Deleting Node with Value 2 Deleting Node with Value 9 Deleting Node with Value 6 Deleting Node with Value 7 Deleting Node with Value 3 Deleting Node with Value 1

**Method 2: Deletion using Level Order Traversal**

We can delete the given binary tree using level order traversal also. In the previous method, we delete the leaf nodes first and then their parents. But, here, we will delete the tree in root to leaf fashion.

Before deleting any node, we will push the left and right child of it, if exist, into queue.

#### C++ Program to Delete Binary Tree is as follows:

```
/* C++ Program to Delete Binary Tree */
#include<bits/stdc++.h>
using namespace std;
typedef struct Node
{
int data;
struct Node *left;
struct Node *right;
Node(int ele)
{
data = ele;
left = NULL;
right = NULL;
}
} Node;
/* Function to Delete Binary Tree */
void deleteTree(Node *root)
{
/* Base Case */
if(root == NULL)
return;
queue<Node *> que;
que.push(root);
while(!que.empty())
{
Node *temp = que.front();
que.pop();
if(temp -> left)
que.push(temp -> left);
if(temp -> right)
que.push(temp -> right);
cout<<"Deleting Node with Value "<<temp -> data<<"\n";
delete temp;
}
root = NULL;
}
int main()
{
/* Creating a Binary trees and inserting some nodes in it */
Node *root = NULL;
root = new Node(1);
root -> left = new Node(2);
root -> right = new Node(3);
root -> left -> left = new Node(4);
root -> left -> right = new Node(5);
root -> right -> left = new Node(6);
root -> right -> right = new Node (7);
root -> left -> right -> left = new Node(8);
root -> right -> left -> right = new Node(9);
/* Calling function to delete binary tree */
deleteTree(root);
}
```

**OUTPUT:**

Deleting Node with Value 1 Deleting Node with Value 2 Deleting Node with Value 3 Deleting Node with Value 4 Deleting Node with Value 5 Deleting Node with Value 6 Deleting Node with Value 7 Deleting Node with Value 8 Deleting Node with Value 9

**Related Posts:**

**Introduction to Tree Data Structure****Binary Tree Traversals****Print All Leaf Nodes of a Binary Tree****Count Number of Nodes in a Binary Tree****Print Alternate Levels of Binary Tree****Maximum Width of Binary Tree****Level Order Tree Traversal****Left View of Binary Tree****Right View of Binary Tree****Compute Height of Binary Tree****Inorder Tree Traversal Using Stack****Preorder Tree Trasversal Using Stack****Postorder Tree Traversal Using Stack****Vertical Order Tree Traversal****Top View of Binary Tree****Bottom View of Binary Tree****Check if two trees are mirror Trees of Each Other****Convert Binary Tree to its Mirror Tree****Check if Binary Tree is Symmetric or Not****Print All Root to Leaf Paths in a Binary Tree**