Remove leaf nodes in Tree

Moderate
0/80
28 upvotes
Asked in companies
Urban Company (UrbanClap)ShareChat

Problem statement

Remove all leaf nodes from a given generic Tree. Leaf nodes are those nodes, which don't have any children.

Note : Root will also be a leaf node if it doesn't have any child. You don't need to print the tree, just remove all leaf nodes and return the updated root.
Detailed explanation ( Input/output format, Notes, Images )
Input format :
Line 1 : Elements in level order form separated by space (as per done in class). Order is - `

Root_data, n (No_Of_Child_Of_Root), n children, and so on for every element `
Output Format :
Elements are printed level wise, each level in new line (separated by space)
Sample Input 1 :
10 3 20 30 40 2 40 50 0 0 0 0 

Input Graph



Input Graph

Sample Output 1 : (Level wise, each level in new line)
10
20
Remove leaf nodes in Tree
Full screen
Console