Remove all leaf nodes from a Generic Tree or N-ary Tree
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 6
Output:
5 : 1 2 3
1 :
2 :
3 :
Explanation:
Deleted leafs are:
8, 15, 4, 5, 6
Input:
8
/ | \
9 7 2
/ | \ | / / | \ \
4 5 6 10 11 1 2 2 3
Output:
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 classclass TreeNode {public: int data; vector<TreeNode*> children; TreeNode(int data) { this->data = data; }};// Recursive function which delete// the leaf from treevoid 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 wisevoid 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 codeint 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
Java
// Java program to delete the// leaf from the generic treeimport java.util.*;class GFG{ // a treenode classstatic class TreeNode { int data; ArrayList<TreeNode> children; TreeNode(int data) { this.data = data; this.children = new ArrayList<>(); }}; // Recursive function which delete// the leaf from treestatic 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.get(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() - 1; j++) root.children.set(j, root.children.get(j + 1)); // delete the last element root.children.remove(root.children.size()-1); 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.get(i)); }} // Function which will print the// tree level wisestatic void printTheTree(TreeNode root){ if (root == null) return; System.out.print(root.data+" :"); for (int i = 0; i < root.children.size(); i++) System.out.print(root.children.get(i).data+" "); System.out.println(); for (int i = 0; i < root.children.size(); i++) printTheTree(root.children.get(i));} // Driver codepublic static void main(String []args){ // 5 // / / \ \ // 1 2 3 8 // / /\ \ // 15 4 5 6 TreeNode root = new TreeNode(5); TreeNode child1 = new TreeNode(1); root.children.add(child1); TreeNode child11 = new TreeNode(15); child1.children.add(child11); TreeNode child2 = new TreeNode(2); root.children.add(child2); TreeNode child21 = new TreeNode(4); TreeNode child22 = new TreeNode(5); child2.children.add(child21); child2.children.add(child22); TreeNode child3 = new TreeNode(3); root.children.add(child3); TreeNode child31 = new TreeNode(6); child3.children.add(child31); TreeNode child4 = new TreeNode(8); root.children.add(child4); removeLeaf(root); printTheTree(root);}}// This code is contributed by rutvik_56 |
chevron_right
filter_none
C#
// C# program to delete the// leaf from the generic treeusing System;using System.Collections;using System.Collections.Generic; class GFG{ // a treenode classclass TreeNode { public int data; public ArrayList children; public TreeNode(int data) { this.data = data; this.children = new ArrayList(); }}; // Recursive function which delete// the leaf from treestatic void removeLeaf(TreeNode root){ // if root.children is a leaf node // then delete it from children vector for (int i = 0; i < root.children.Count; i++) { TreeNode child= (TreeNode)root.children[i]; // if it is a leaf if (child.children.Count == 0) { // shifting the vector to left // after the point i for (int j = i; j < root.children.Count - 1; j++) { root.children[j]= root.children[j + 1]; } // delete the last element root.children.RemoveAt(root.children.Count - 1); i--; } } // Remove all leaf node // of children of root for (int i = 0; i < root.children.Count; i++) { // call function for root.children removeLeaf((TreeNode)root.children[i]); }} // Function which will print the// tree level wisestatic void printTheTree(TreeNode root){ if (root == null) return; Console.Write(root.data+" :"); for (int i = 0; i < root.children.Count; i++) Console.Write(((TreeNode)root.children[i]).data + " "); Console.WriteLine(); for (int i = 0; i < root.children.Count; i++) printTheTree((TreeNode)root.children[i]);} // Driver codepublic static void Main(string []args){ // 5 // / / \ \ // 1 2 3 8 // / /\ \ // 15 4 5 6 TreeNode root = new TreeNode(5); TreeNode child1 = new TreeNode(1); root.children.Add(child1); TreeNode child11 = new TreeNode(15); child1.children.Add(child11); TreeNode child2 = new TreeNode(2); root.children.Add(child2); TreeNode child21 = new TreeNode(4); TreeNode child22 = new TreeNode(5); child2.children.Add(child21); child2.children.Add(child22); TreeNode child3 = new TreeNode(3); root.children.Add(child3); TreeNode child31 = new TreeNode(6); child3.children.Add(child31); TreeNode child4 = new TreeNode(8); root.children.Add(child4); removeLeaf(root); printTheTree(root);}}// This code is contributed by pratham76 |
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.

