Given a Generic tree, the task is to delete the leaf nodes from the tree.
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
- Recursively call for every child.
Below is the implementation of the above approach:
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.
- Remove nodes from Binary Tree such that sum of all remaining root-to-leaf paths is atleast K
- Convert a Generic Tree(N-array Tree) to Binary Tree
- Print the longest leaf to leaf path in a Binary tree
- Remove nodes on root to leaf paths of length < K
- 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
- Print all leaf nodes of a Binary Tree from left to right
- Sum of all leaf nodes of 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
- Replace every node with depth in N-ary Generic Tree
- Height of a generic tree from parent array
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Print all nodes that are at distance k from a leaf node
- Print all root-to-leaf paths with maximum count of even nodes
- Implementing Generic Graph in Java
- Generic Trees(N-array Trees)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.