forked from fnplus/interview-techdev-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
714a454
commit 4300a4c
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |