

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Delete leaf nodes with value as x in C++?
Let us first define the struct that would represent a tree node that contains the data and its left and right node child. If this is the first node to be created then it’s a root node otherwise a child node.
struct Node { int data; struct Node *leftChild, *rightChild; };
Next we create our newNode(int data) function that takes an int value and assign it to the data member of the node. The function returns the pointer to the created struct Node. Also the left and right child of the newly created node are set to null.
struct Node* newNode(int data){ struct Node* newNode = new Node; newNode->data = data; newNode->leftChild = newNode->rightChild = NULL; return (newNode); }
Now we create our deleteNode(Node* root, int x) function which takes the root node and the data value of the node to be deleted. If the given node is a parent node then it also deletes its left and right child. The function returns the modified root node after deletion of the given node.
Node* deleteLeafNode(Node* root, int x){ if (root == NULL) return nullptr; root->leftChild = deleteLeafNode(root->leftChild, x); root->rightChild = deleteLeafNode(root->rightChild, x); if (root->data == x && root->leftChild == NULL && root->rightChild == NULL) return nullptr; return root; }
Finally for dispaying the tree after deletion we have a function inorder(Node* root) which traverses the tree in inorder function.
void inorder(Node* root){ if (root != NULL){ inorder(root->leftChild); inorder(root->rightChild); cout << root->data << " "; } }
Example
Let us see the following implementation of deleting leaf nodes that have value equal to x
#include <iostream> using namespace std; struct Node { int data; struct Node *leftChild, *rightChild; }; struct Node* newNode(int data){ struct Node* newNode = new Node; newNode->data = data; newNode->leftChild = newNode->rightChild = NULL; return (newNode); } Node* deleteNode(Node* root, int x){ if (root == NULL) return nullptr; root->leftChild = deleteNode(root->leftChild, x); root->rightChild = deleteNode(root->rightChild, x); if (root->data == x && root->leftChild == NULL && root->rightChild == NULL) return nullptr; return root; } void inorder(Node* root){ if (root != NULL){ inorder(root->leftChild); inorder(root->rightChild); cout << root->data << " "; } } int main(void){ struct Node* root = newNode(4); root->leftChild = newNode(2); root->rightChild = newNode(12); root->leftChild->leftChild = newNode(3); root->leftChild->rightChild = newNode(5); root->rightChild->rightChild = newNode(9); deleteNode(root, 3); cout << "Inorder traversal after deletion : "; inorder(root); return 0; }
Output
The above code will produce the following output −
Inorder traversal after deletion : 5 2 9 12 4
- Related Questions & Answers
- Delete leaf nodes with value as x in C++ Program
- Delete leaf nodes with value k in C++?
- Delete leaf nodes with value k in C++ program
- Print the nodes of binary tree as they become the leaf node in C++ Programming.
- Delete Tree Nodes in C++
- Program to find leaf and non-leaf nodes of a binary tree in Python
- Insufficient Nodes in Root to Leaf Paths in Python
- C++ Pairwise Swap Leaf Nodes in a Binary Tree
- Delete all the nodes from the list that are greater than x in C++
- Count Non-Leaf nodes in a Binary Tree in C++
- Print Leaf Nodes at a given Level in C language
- Product of all leaf nodes of binary tree in C++
- Delete Nodes And Return Forest in Python
- Delete N nodes after M nodes of a linked list in C++?
- Print all nodes less than a value x in a Min Heap in C++