For a rooted tree with any arbitary number of children for each node, not necessarily n-ary tree.
Remove all the leaf nodes, and store them in a list, this would create new leaf nodes. Repeat untill all the nodes are removed
Conditions : Freshly created leaf nodes(node whose children are removed) should not be removed just after its children are removed, unless there's no other option for us, then we can remove it
For Example
1
2 3 4
5 6 8 7
Following is a tree rooted at 1, with 2,3,4 as its children. 2 has children 5,6,8 and 3 has children 7
One Possible output for the given input would be,
5 6 8 7 4 2 3 1
Explanation : Remove 5 & 6 since they are leaf nodes, Now 2 is a freshly created leaf
Since we can process other nodes, we will not remove it
Next Leaf removed is 7, After this newly created 3 cannot be removed
Hence Remove 4, after that since it is a leaf
Now Remove 2, after which remove 3
1 is now a freshly created leaf, but since there is no other options remaining, remove 1