- Related Questions & Answers
- Average numbers in array in C Programming
- Add minimum number to an array so that the sum becomes even in C programming
- Arithmetic Mean in C programming
- C Program for Tower of Hanoi
- C++ Program for Dijkstra’s shortest path algorithm?
- C/C++ Program for nth Catalan Number?
- C Program to Multiply two Floating Point Numbers?
- C program to find the length of a string?
- C Program to Check if all digits of a number divide it
- C Program to Check if a Given String is a Palindrome?
- Concatenate a string given number of times in C++ programming
- Write a function that returns 2 for input 1 and returns 1 for 2 in C programming
- Write a C program to print “ Tutorials Point ” without using a semicolon
- Ways to paint N paintings such that adjacent paintings don’t have same colors in C programming
- Surface Area and Volume of Hexagonal Prism in C programming

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

To delete a tree we need to traverse each node of the tree and then delete each of them. this one by one delete every node of the tree and makes it empty. For this, we need to use a method that traverses the tree from bottom to up so that we can delete the lower notes first and then their parents so that no extra complexities arise. Based on the condition that we need, The postorder traversal will be the best suited And works efficiently so that our program will be optimum.

The post-order for the following tree is -

2-6-4-12-17-15

The postorder travel cell technique works in by the following way -

checks for the left childnode → check for the rootnode → chekcs the right childnode

#include<stdio.h> #include<stdlib.h> struct node { int data; struct node* left; struct node* right; }; struct node* addnode(int data) { struct node* node = (struct node*) malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } void nodedel(struct node* node) { if (node == NULL) return; nodedel(node->left); nodedel(node->right); printf("\n Node deleted, value is %d", node->data); free(node); } int main() { struct node *root = addnode(9); root->left = addnode(4); root->right = addnode(15); root->left->left = addnode(2); root->left->right = addnode(6); root->right->left = addnode(12); root->right->right = addnode(17); nodedel(root); root = NULL; printf("\n Tree deleted "); return 0; }

Node deleted, value is 4 Node deleted, value is 12 Node deleted, value is 17 Node deleted, value is 15 Node deleted, value is 9 Tree deleted

Advertisements