Given a **Generic tree**, the task is to **delete** the leaf nodes from the **tree**.

** Examples:**

Input:5 / / \ \ 1 2 3 8 / / \ \ 15 4 5 6Output:5 : 1 2 3 1 : 2 : 3 :Explanation:Deleted leafs are: 8, 15, 4, 5, 6Input: 8 / | \ 9 7 2 / | \ | / / | \ \ 4 5 6 10 11 1 2 2 3Output:8: 9 7 2 9: 7: 2:

**Approach: **Follow the steps given below to solve the problem

- Take tree into the
**vector**. - Traverse the tree and check the condition:
- If current node is leaf then
- Delete the leaf from vector

- Else
- Recursively call for every child.

Below is the implementation of the above approach:

## C++

`// C++ program to delete the ` `// leaf from the generic tree ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// a treenode class ` `class` `TreeNode { ` `public` `: ` ` ` `int` `data; ` ` ` `vector<TreeNode*> children; ` ` ` ` ` `TreeNode(` `int` `data) ` ` ` `{ ` ` ` `this` `->data = data; ` ` ` `} ` `}; ` ` ` `// Recursive function which delete ` `// the leaf from tree ` `void` `removeLeaf(TreeNode* root) ` `{ ` ` ` `// if root->children is a leaf node ` ` ` `// then delete it from children vector ` ` ` `for` `(` `int` `i = 0; ` ` ` `i < root->children.size(); ` ` ` `i++) { ` ` ` ` ` `TreeNode* child ` ` ` `= root->children[i]; ` ` ` ` ` `// if it is a leaf ` ` ` `if` `(child->children.size() == 0) { ` ` ` ` ` `// shifting the vector to left ` ` ` `// after the point i ` ` ` `for` `(` `int` `j = i; ` ` ` `j < root->children.size(); ` ` ` `j++) ` ` ` `root->children[j] ` ` ` `= root->children[j + 1]; ` ` ` ` ` `// delete the last element ` ` ` `root->children.pop_back(); ` ` ` ` ` `i--; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Remove all leaf node ` ` ` `// of children of root ` ` ` `for` `(` `int` `i = 0; ` ` ` `i < root->children.size(); ` ` ` `i++) { ` ` ` ` ` `// call function for root->children ` ` ` `removeLeaf(root->children[i]); ` ` ` `} ` `} ` ` ` `// Function which will print the ` `// tree level wise ` `void` `printTheTree(TreeNode* root) ` `{ ` ` ` `if` `(root == NULL) ` ` ` `return` `; ` ` ` ` ` `cout << root->data << ` `" "` ` ` `<< ` `":"` `; ` ` ` `for` `(` `int` `i = 0; ` ` ` `i < root->children.size(); ` ` ` `i++) ` ` ` `cout << root->children[i]->data ` ` ` ` ` `<< ` `" "` `; ` ` ` `cout << endl; ` ` ` ` ` `for` `(` `int` `i = 0; ` ` ` `i < root->children.size(); ` ` ` `i++) ` ` ` `printTheTree(root->children[i]); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// 5 ` ` ` `// / / \ \ ` ` ` `// 1 2 3 8 ` ` ` `// / /\ \ ` ` ` `// 15 4 5 6 ` ` ` ` ` `TreeNode* root = ` `new` `TreeNode(5); ` ` ` `TreeNode* child1 = ` `new` `TreeNode(1); ` ` ` `root->children.push_back(child1); ` ` ` `TreeNode* child11 = ` `new` `TreeNode(15); ` ` ` `child1->children.push_back(child11); ` ` ` `TreeNode* child2 = ` `new` `TreeNode(2); ` ` ` `root->children.push_back(child2); ` ` ` `TreeNode* child21 = ` `new` `TreeNode(4); ` ` ` `TreeNode* child22 = ` `new` `TreeNode(5); ` ` ` `child2->children.push_back(child21); ` ` ` `child2->children.push_back(child22); ` ` ` `TreeNode* child3 = ` `new` `TreeNode(3); ` ` ` `root->children.push_back(child3); ` ` ` `TreeNode* child31 = ` `new` `TreeNode(6); ` ` ` `child3->children.push_back(child31); ` ` ` `TreeNode* child4 = ` `new` `TreeNode(8); ` ` ` `root->children.push_back(child4); ` ` ` ` ` `removeLeaf(root); ` ` ` `printTheTree(root); ` `}` |

*chevron_right*

*filter_none*

**Output:**

5 :1 2 3 1 : 2 : 3 :

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Print the longest leaf to leaf path in a Binary tree
- Remove nodes on root to leaf paths of length < K
- Sum of all leaf nodes of binary tree
- Print all leaf nodes of a Binary Tree from left to right
- Product of all leaf nodes of binary tree
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Print all leaf nodes of a binary tree from right to left
- Maximum sum of leaf nodes among all levels of the given binary tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Print all leaf nodes of an n-ary tree using DFS
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Height of a generic tree from parent array
- Replace every node with depth in N-ary Generic Tree
- Print all nodes that are at distance k from a leaf node
- Program to count leaf nodes in a binary tree
- Find height of a special binary tree whose leaf nodes are connected
- Iterative program to count leaf nodes in a Binary Tree
- Pairwise Swap leaf nodes in a binary tree
- Count Non-Leaf nodes in a Binary Tree

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.