Skip to content

Commit

Permalink
DeleteTree
Browse files Browse the repository at this point in the history
  • Loading branch information
NripeshKumar committed Oct 11, 2019
1 parent 714a454 commit 4300a4c
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Algorithms/Tree/deleteTree/cpp/deleteTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include<bits/stdc++.h>
#include<iostream>
using namespace std;

/* A binary tree node has data,
pointer to left child and
a pointer to right child */
class node
{
public:
int data;
node* left;
node* right;

/* Constructor that allocates
a new node with the given data
and NULL left and right pointers. */
node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};


/* This function traverses tree
in post order to delete each
and every node of the tree */
void deleteTree(node* node)
{
if (node == NULL) return;

/* first delete both subtrees */
deleteTree(node->left);
deleteTree(node->right);

/* then delete the node */
cout << "\n Deleting node: " << node->data;
free(node);
}


/* Driver code*/
int main()
{
node *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);

deleteTree(root);
root = NULL;

cout << "\n Tree deleted ";

return 0;
}

//This code is contributed b

0 comments on commit 4300a4c

Please sign in to comment.